0

I am inserting data into my entity table using .AddObject(). The object is of the entity table's type. The object is eventStudent, it has string eventStudent.ID, bool eventStudent.StudentPresent, bool eventStudent.ParentPresent.

The students are a list of strings containing student ids. Their presence at the event is in another object called attendees, consisting of String studentID, bool studentPresent and bool parentPresent. Only student id's that have true for StudentPresent and/or ParentPresent are in the attendees list. As I load up my eventStudent object, I need to set StudentPresent and ParentPresent. This is what I came up with:

foreach (StudentMinimum student in students)
{
eventStudent.StudentPresent = (from a in attendees
                           where a.StudentID.Contains(student.StudentID)
                           && a.StudentPresent
                           select a.StudentPresent);
}

I receive the error cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'bool'

How can I improve my query so eventStudent.StudentPresent is set to either True or False?

Jazzy
  • 519
  • 9
  • 31

1 Answers1

0

The compiler doesn't know what type will be returned from your query as you haven't explicitly casted it to a type. As a result, it gets a generic IEnumerable type (there could be many records returned right? Hence the IEnumerable. And those records could each be of any type, hence the generic type).

So if the data in your DB were bad and you got multiple records back, converting:

StudentPresent
true
false
false

to a bool is not going to happen. There are a few ways you could get around this. Personally, I'd do something like

var studentPresent = (from a in attendees
                           where a.StudentID.Contains(student.StudentID)
                           && a.StudentPresent
                           select a.StudentPresent).FirstOrDefault();

eventStudent.StudentPresent = (bool)studentPresent;

Well, actually, I'd use a lambda query instead but that's just personal preference.

Andrew
  • 307
  • 7
  • 13
Rodders
  • 2,425
  • 2
  • 20
  • 34