如何在EF LINQ中联接表 [英] How to join tables in EF LINQ

查看:124
本文介绍了如何在EF LINQ中联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试联接表时

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()

我收到这样的错误

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

推荐答案

试试看:

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

无论如何,我建议您在EDM设计器中对Foo和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;

这篇关于如何在EF LINQ中联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆