Shaping EF LINQ Query Results Using Multi-Table Includes

Posted by sisdog on Stack Overflow See other posts from Stack Overflow or by sisdog
Published on 2011-01-17T02:47:23Z Indexed on 2011/01/17 2:53 UTC
Read the original article Hit count: 207

Filed under:
|

I have a simple LINQ EF query below using the method syntax. I'm using my Include statement to join four tables: Event and Doc are the two main tables, EventDoc is a many-to-many link table, and DocUsage is a lookup table.

My challenge is that I'd like to shape my results by only selecting specific columns from each of the four tables. But, the compiler is giving a compiler is giving me the following error:

'System.Data.Objects.DataClasses.EntityCollection does not contain a definition for "Doc' and no extension method 'Doc' accepting a first argument of type 'System.Data.Objects.DataClasses.EntityCollection' could be found.

I'm sure this is something easy but I'm not figuring it out. I haven't been able to find an example of someone using the multi-table include but also shaping the projection.

Thx,Mark

var qry= context.Event
    .Include("EventDoc.Doc.DocUsage")
    .Select(n => new
    {
        n.EventDate,
        n.EventDoc.Doc.Filename,  //<=COMPILER ERROR HERE
        n.EventDoc.Doc.DocUsage.Usage
    })
    .ToList();

    EventDoc ed;
    Doc d = ed.Doc;  //<=NO COMPILER ERROR SO I KNOW MY MODEL'S CORRECT
    DocUsage du = d.DocUsage;

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about entity-framework