I'm new to LINQ so apologies if this is a simple answer. I'm trying to do a SQL join and have the following code based on examples I've seen on SO and elsewhere:
var query = from e in db.Events
join ec in db.EventCategories on e.ID equals ec.EventID
join c in db.Categories on ec.CategoryCode equals c.CategoryCode
join ep in db.EventParticipants on e.ID equals ep.EventID
join p in db.Participants on ep.ParticipantCode equals p.ParticipantCode
select new { e, ec, c, ep, p };
This executes fine and when I run the debugger I can expand the Object and see that the query ran successfully. However, when I try to execute query.ToList() I can't cast this into anything usable because it says that the list returned by query.ToList() is System.Collections.Generic.List.
Method #2: Based on my own thoughts I tried creating the following struct:
public struct CalendarItem
{
public Event e;
public EventCategory ec;
public Category c;
public EventParticipant ep;
public Participant p;
public CalendarItem(Event E, EventCategory EC, Category C, EventParticipant EP, Participant P)
{
e = E;
ec = EC;
c = C;
ep = EP;
p = P;
}
}
And then modifying the LINQ command to the following:
var query = from e in db.Events
join ec in db.EventCategories on e.ID equals ec.EventID
join c in db.Categories on ec.CategoryCode equals c.CategoryCode
join ep in db.EventParticipants on e.ID equals ep.EventID
join p in db.Participants on ep.ParticipantCode equals p.ParticipantCode
select new CalendarItem(e, ec, c, ep, p);
In Visual Studio this checks out and it allows me to compile and everything looks great (i.e. I can loop over the list of CalendarItems), but I get a runtime error on query.ToList(): The member 'e' has no supported translation to SQL.