How to join tables in EF LINQ

别说谁变了你拦得住时间么 提交于 2019-11-29 13:29:51

问题


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;


query.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.

回答1:


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;


来源:https://stackoverflow.com/questions/5112183/how-to-join-tables-in-ef-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!