开发者

How to join tables in EF LINQ

开发者 https://www.devze.com 2023-02-13 02:53 出处:网络
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

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;


quer开发者_开发知识库y.toArray()

I get such error

Unable to create a constant value of type 'Bar'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.


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

精彩评论

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

关注公众号