c# - 使用 IN 运算符的子查询列出 Entity Framework 6

标签 c# sql entity-framework asp.net-mvc-5

我想使用 IN 运算符在 Entity Framework 中执行子查询,然后使用 as 列表发送到 View 。

这是我的SQL代码

select * from famProd where id in (Select famID from appNfam where appID = 1)

这是我的 Controller

public ActionResult ProductFamily(int id)
    {
        Session["appID"] = id;

        var famProds = from prodFam in famProd where prodFam.id == (from piv in appNfam where piv.appID  == id);



        return View();

    }

希望你能帮助我

最佳答案

尝试这样的事情:-

var appIds   = from piv in appNfam where piv.appID  == id;
var famProds = from p in DbContext.famProd
            where appIds.Contains(p.id)
            select p;  

关于c# - 使用 IN 运算符的子查询列出 Entity Framework 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46228229/

相关文章:

c# - ASP+EF加载程序集问题

entity-framework - 您对 Entity Framework 有何看法?

c# - 替换 Windows Phone 中的序列化信息

c# - 什么时候适合使用 KnownType 属性?

sql - 如何合并 2 个 SQL 查询?

sql - SQL 查询连接

sql - 如何选择没有 ID 字段的 SQL 表的最后 10 行?

c# - 在类/子类中使用 Content.Load<>

c# - 如何使用两个不同版本的 Visual Studio 处理 TFS DLL

c# - EF - 处理多个多对多映射的正确方法