开发者

How to use a JOIN in a LINQ query

I have the following table structure, which is imported into an Entity Framework project: (NOTE: I mislabled T1Name as T2Name in Table1)

alt text http://digitalsamurai.us/images/drawing2.png

I have labeled the Entity Objects. The many-to-many joint table Table5, is represented as an EntityCollection<Entity4> in Entity3, and as an EntityCollection<Entity3> in Entity4 (EntityCollection<开发者_开发技巧T> implements IEnumerable<T>, so it can be queried). I need to construct a result set that is:

T1Name, T2Name, T3Name

This will result in repeat entries for T1Name, and T2Name.

Could someone show me how to write this LINQ query?

Thank you for any help.


var q = from e3 in Context.Table3
        from e4 in e3.Table4s     // that's your many-to-many
        select new
        {
            Name3 = e3.T3Name,
            Name2 = e4.Table2.T2Name,
            Name1 = e4.Table1.T1Name // presuming Table1.T2Name in your diagram is a typo
        };

"Dot notation":

var q = Context.Table3
               .SelectMany(e3 => e3.Select(e4 => 
                                               new {
                                                   Name3 = e3.T3Name,
                                                   Name2 = e4.Table2.T2Name,
                                                   Name1 = e4.Table1.T1Name
                                               });

Notice I didn't use join at all. That's on purpose; you don't need it here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜