How to join tables in EF LINQ

前端 未结 1 1293
耶瑟儿~
耶瑟儿~ 2020-12-31 10:31

When I try to join tables

var query =
    from foo in db.Foos
    from bar in db.Bars
    where foo.ID == bar.FooID
    where foo.ID == 45
    select bar;


         


        
相关标签:
1条回答
  • 2020-12-31 10:39

    Try that instead:

    var query =
        from foo in db.Foos
        join bar in db.Bars on foo.ID equals bar.FooID
        where foo.ID == 45
        select bar;
    

    Anyway, I suggest you model the relation between Foo and Bar in the EDM designer, this way you don't need an explicit join:

    var query =
        from foo in db.Foos
        where foo.ID == 45
        from bar in foo.Bars
        select bar;
    
    0 讨论(0)
提交回复
热议问题