本文介绍了如何在 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 中连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 16:36