简体   繁体   中英

How to search data from multiple tables using in EF

Here I want to search data into multiple tables. I have 3 tables

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     |
|--------------------------|

now here I have various search parameters and to get data I am writing joins as below.

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

this query doesn't return accurate result as In my DB there are 400+ entries which has the Status "New" but this query returns only 15 entries with some repeatative records. So for searching data from multiple tables with multiple search parameter should I use Joins?

In Linq you seldom need to use join, provided you have set relations in your database and model reflects those relations (navigation properties). Also in your query Categories and IntakeRes don't seem to have a role other than creating an "inner join" which serves as an exists query. Is that really you want (results would be filtered when there is no match). If not:

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

If that was the intended result then:

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

Whats the purpose of the join in here? you arent using the joined tables in neither your where or select clauses.

As already mentioned in an answer, the joins will serve as EXIST clause, so most likely the missing entries in your Incident table dont have associated entries according to your join statements in either Intake or Category tables. If you want to include the incident entries without matches in either intake or category tables, you will need to use a left join.

The fact that you are getting "duplicated" entries is a direct consequence of using join, as it will create 1 row for every {Incident, Category, Intake} matching tuple.

If you are working with entity framework, you might as well simply use the navigation properties defined in your models:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM