如何在EF中使用多个表搜索数据

时间:2019-09-17 11:34:58

标签: c# entity-framework linq join

在这里我想将数据搜索到多个表中。 我有3张桌子

const payment1: string = '1000';
const payment2: string = '1001';

const paymentArray: string[] = [];
paymentArray.push(payment1, payment2);

const payments$ = of(['1000', '1001', '1002']);

const allExist = await from(paymentArray).pipe(withLatestFrom(payments$), every(([item, payments]) => payments.includes(item))).toPromise();

现在在这里,我有各种搜索参数,并且要获取要写入的数据,如下所示。

1)Incident
Inc_Id |Name| Status | IncidentNumber | Location
-----------------------------------------------------
| 1     | abc | New   | 0001           | Location1  |
|---------------------------------------------------|
| 2     | pqr | Closed |0002           | Location 2 |
-----------------------------------------------------

2) Category
Id | Name   | Inc_Id 
-------------------
| 1 | cate1 |  1   |
|------------------|
| 2 | cat2  |  1   |
|------------------|
|3  | cat3  |  2   |
|------------------|

3) Intake
 Id | manager_Name | Inc_id
---------------------------
|1 |  name1        | 1     |
|--------------------------|
|2 | name 2        | 2     |
|--------------------------|

此查询未返回准确的结果,因为在我的数据库中,有400多个条目的状态为“新建” ,但此查询仅返回15条带有重复记录的条目。 因此,要从具有多个搜索参数的多个表中搜索数据,我应该使用联接吗?

2 个答案:

答案 0 :(得分:0)

如果您在数据库中设置了关系并且模型可以反映这些关系(导航属性),则在Linq中,您几乎不需要使用联接。另外,在您的查询中,类别和IntakeRes似乎没有其他作用,除了创建用作现有查询的“内部联接”之外。确实是您想要的(如果没有匹配项,结果将被过滤)。如果没有:

var searchResult = (from incident in db.Incident
                    where (!string.IsNullOrEmpty(filters.Location)
                           ? incident.Location == filters.Location
                           : incident.IncidentNumber != null)
                           &&
                           (filters.Status != null
                            ? incident.Status == filters.Status
                            : incident.IncidentNumber != null)
                            select new MyList
                            {
                                IncidentId = incident.Inc_Id,
                                IncidentNumber = incident.IncidentNumber,
                                Location = incident.Location
                             }).ToList();

如果这是预期的结果,那么:

var searchResult = (from incident in db.Incident
                    where incident.Category.Any() && incident.Intake.Any() &&
                          (!string.IsNullOrEmpty(filters.Location)
                           ? incident.Location == filters.Location
                           : incident.IncidentNumber != null)
                           &&
                           (filters.Status != null
                            ? incident.Status == filters.Status
                            : incident.IncidentNumber != null)
                            select new MyList
                            {
                                IncidentId = incident.Inc_Id,
                                IncidentNumber = incident.IncidentNumber,
                                Location = incident.Location
                             }).ToList();

答案 1 :(得分:0)

加入这里的目的是什么?您不能在where或select子句中使用联接表。

正如在答案中已经提到的,联接将用作EXIST子句,因此根据IntakeCategory中的联接语句,事件表中缺少的条目很可能没有关联的条目表。如果要在进气口或类别表中包含不匹配的事件条目,则需要使用左联接。

获取“重复”条目的事实是使用连接的直接结果,因为它将为每个{事件,类别,摄入量}匹配的元组创建1行。

如果您使用的是实体框架,则不妨使用模型中定义的导航属性:

var searchResult = (from incident in db.Incident
                    where incident.Category.Any() && incident.Intake.Any() &&
                          (!string.IsNullOrEmpty(filters.Location)
                           ? incident.Location == filters.Location
                           : incident.IncidentNumber != null)
                           &&
                           (filters.Status != null
                            ? incident.Status == filters.Status
                            : incident.IncidentNumber != null)
                            select new MyList
                            {
                                IncidentId = incident.Inc_Id,
                                IncidentNumber = incident.IncidentNumber,
                                Location = incident.Location
                             }).ToList();
相关问题