实体框架查询-从子表返回唯一行 [英] Entity Framework query - return unique rows from child table

查看:49
本文介绍了实体框架查询-从子表返回唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有如下所示的Entity Framework模型以及相应数据库表中的数据:

I have Entity Framework model as below and data in the corresponding database tables:

问题:如何查询模型以返回与给定person相关的sales_type中的所有行(完整实体,不是匿名类型)?

Question: How do I query the model for returning all the rows (full entities, not anonymous type) from sales_type relevant to a given person ?

如果数据如上,则对于John,查询应从sales_type-表返回前三行,对于Mark,查询应返回第2和5行.

If the data is as above, then for John, the query should return first three rows from sales_type-table and for Mark it should return the rows 2 and 5.

推荐答案

也许是这样的:

var personId=1;
var result= (
        from salesType in db.sales_type
        where
            (
                from salesMapping in db.sales_mapping
                join personSale in db.person_Sales
                    on salesMapping.id equals personSale.sales_mapping_id
                join person in db.person
                    on personSale.person_id equals person.id
                where
                    person.Id == personId
                select salesMapping.sales_type_id
            ).Contains(salesType.id)
        select salesType
    ).ToList();

其中db是linq数据上下文

Where db is the linq data context

这篇关于实体框架查询-从子表返回唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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