Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用in-EF从多个表中搜索数据_C#_Entity Framework_Linq_Join - Fatal编程技术网

C# 如何使用in-EF从多个表中搜索数据

C# 如何使用in-EF从多个表中搜索数据,c#,entity-framework,linq,join,C#,Entity Framework,Linq,Join,这里我想将数据搜索到多个表中。 我有三张桌子 1)Incident Inc_Id |Name| Status | IncidentNumber | Location ----------------------------------------------------- | 1 | abc | New | 0001 | Location1 | |---------------------------------------------------| | 2

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

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     |
|--------------------------|
现在我有了各种搜索参数,为了获得数据,我正在编写连接,如下所示

 var searchResult = new List<MyList>();
    searchResult = (from incident in db.Incident
                            join categories in db.Category on incident.Inc_Id equals categories.Inc_Id
                            join intakeRes in db.Intake on incident.Inc_Id equals intakeRes.Inc_Id
where
                              (!string.IsNullOrEmpty(filters.Location)
                              ? incident.Location == filters.Location && !string.IsNullOrEmpty(incident.Location)
                              : incident.IncidentNumber != null)
                              &&

                              (filters.Status != null
                              ? incident.Status == filters.Status && !string.IsNullOrEmpty(incident.Status)
                              : incident.IncidentNumber != null)
                              select new MyList
                            {
                                IncidentId = incident.Inc_Id,
                                IncidentNumber = incident.IncidentNumber,
                                Location = incident.Location
                             }).ToList();
var searchResult=new List();
searchResult=(来自数据库中的事件)
连接数据库中的类别。incident.Inc_Id上的类别等于categories.Inc_Id
将进水口加入db。incident.Inc_Id上的进水口等于进水口
哪里
(!string.IsNullOrEmpty(filters.Location)
?incident.Location==filters.Location&&!string.IsNullOrEmpty(incident.Location)
:incident.IncidentNumber!=null)
&&
(filters.Status!=null
?incident.Status==filters.Status&&!string.IsNullOrEmpty(incident.Status)
:incident.IncidentNumber!=null)
选择新建MyList
{
IncidentId=事件公司Id,
IncidentNumber=事件。IncidentNumber,
地点=事故地点
}).ToList();
此查询不会返回准确的结果,因为在my DB中有400多个条目的状态为“新建”,但此查询仅返回15个条目,其中包含一些重复记录。
因此,对于使用多个搜索参数从多个表中搜索数据,我应该使用联接吗

在Linq中,只要在数据库中设置了关系并且模型反映了这些关系(导航属性),就很少需要使用join。此外,在您的查询中,除了创建用作现有查询的“内部联接”之外,类别和Intaker似乎没有其他作用。这是您真正想要的(当没有匹配项时,结果将被过滤)。如果没有:

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

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

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

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

得到“重复”条目的事实是使用join的直接结果,因为它将为每个{Incident,Category,Intaction}匹配元组创建一行

如果您使用的是实体框架,那么您最好只使用模型中定义的导航属性:

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

实际上,我还希望从join query中提到的表中获得一些数据,但我没有提到这些,因为当传递的参数数据应该只来自单个表时,我无法获得准确的数据,即事件table@Madhav,然后只需通过导航属性从这些表中添加值。ie:Category=incident.Category.Name。如果Category或intraction为null,则可能需要使用左外联接。看,我尝试了左外连接,这会返回额外的记录
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();