Search through multiple fields/joins in Linq to Entity - linq

I'm trying to search for matching people via a query like using entity framework:
var ret = from s in Lab_SubSpaces
join w1 in Workers on s.Alt_Contact equals w1.WWID
where SqlMethods.Like(w1.Full_Name, search) || SqlMethods.Like(w1.IDSID, search)
select s.Lab_Space_Id;
My problem now is that I have 5 more columns which I have to search against. So I think that means I need 6 more LEFT joins there as well. I really have no concept of how to do that as the joins keep expanding out and I get lost on the left joins with linq.
This is the SQL call that I need to end up replicating:
SELECT DISTINCT Lab_Space_Id
FROM Lab_SubSpace
LEFT JOIN Worker AS w1 ON Alt_Contact = w1.WWID
LEFT JOIN Worker AS w2 ON Capital_Contact = w2.WWID
LEFT JOIN Worker AS w3 ON PPHW_Contact = w3.WWID
LEFT JOIN Worker AS w4 ON IT_Contact = w4.WWID
LEFT JOIN Worker AS w5 ON Main_Contact = w5.WWID
LEFT JOIN Worker AS w6 ON Safety_Contact = w6.WWID
WHERE w1.Full_Name LIKE #search OR w1.IDSID LIKE #search OR
w2.Full_Name LIKE #search OR w2.IDSID LIKE #search OR
w3.Full_Name LIKE #search OR w3.IDSID LIKE #search OR
w4.Full_Name LIKE #search OR w4.IDSID LIKE #search OR
w5.Full_Name LIKE #search OR w5.IDSID LIKE #search OR
w6.Full_Name LIKE #search OR w6.IDSID LIKE #search

OK, I figured this out. As Gert pointed out if relationships were setup this wouldn't be necessary, but I'm posting still for people just doing Linq-to-SQL that stumble across this.
var byContactName = from s in context.Lab_SubSpace.AsNoTracking()
from w1 in context.Worker.Where(w => w.WWID == s.Alt_Contact).DefaultIfEmpty()
from w2 in context.Worker.Where(w => w.WWID == s.Main_Contact).DefaultIfEmpty()
from w3 in context.Worker.Where(w => w.WWID == s.IT_Contact).DefaultIfEmpty()
from w4 in context.Worker.Where(w => w.WWID == s.PPHW_Contact).DefaultIfEmpty()
from w5 in context.Worker.Where(w => w.WWID == s.Capital_Contact).DefaultIfEmpty()
from w6 in context.Worker.Where(w => w.WWID == s.Technical_Contact).DefaultIfEmpty()
where w1.Full_Name.ToLower().Contains(text) || w1.IDSID.ToLower().Contains(text) ||
w2.Full_Name.ToLower().Contains(text) || w2.IDSID.ToLower().Contains(text) ||
w3.Full_Name.ToLower().Contains(text) || w3.IDSID.ToLower().Contains(text) ||
w4.Full_Name.ToLower().Contains(text) || w4.IDSID.ToLower().Contains(text) ||
w5.Full_Name.ToLower().Contains(text) || w5.IDSID.ToLower().Contains(text) ||
w6.Full_Name.ToLower().Contains(text) || w6.IDSID.ToLower().Contains(text)
select s.Lab_Space_Id;

Related

Left joining a table's id with the maximum id from a subquery

I am using .NET core and trying to write a Linq query to list Cases. I have 3 tables that I am trying to join together:
Cases - Includes CaseID and CaseStatusID
CaseStatuses - Includes CaseStatusID (a Case has one of these)
CaseStatusHistory - Includes CaseStatusHistoryID, CaseID, CaseStatusID (a Case has many of these).
I can join Cases and CaseStatuses easily on CaseStatusID but am not sure how to join CaseStatusHistory on CaseStatusHistoryID = a subquery selecting the MAX(CaseStatusHistoryID) WHERE CaseID and CaseStatusID matches. I could even select the CaseStatusHitory later in my results but am not sure how.
This is what I need to convert
SELECT
c.CaseID,
csh.DateTimeAdded
FROM dbo.Cases c
JOIN dbo.CaseStatuses cs
ON c.CaseStatusID = cs.CaseStatusID
LEFT JOIN dbo.CaseStatusHistory csh
ON csh.CaseStatusHistoryID =
(
SELECT MAX(CaseStatusHistoryID)
FROM dbo.CaseStatusHistory
WHERE CaseID = c.CaseID
AND cs.CaseStatusID = c.CaseStatusID
)
This is what I have so far in Linq
IQueryable<CasesViewModel> objs =
from c in _db.Cases
from cs in _db.CaseStatuses.Where(cs => cs.CaseStatusId == c.CaseStatusId.Value).DefaultIfEmpty()
select new CasesViewModel
{
CaseID = c.CaseId,
CaseStatus = cs.CaseStatus
//StatusChangeDate = csh.DateTimeAdded
};
I need to add something like this
From csh In CaseStatusHistories
Where
(
From _csh In CaseStatusHistories
Where _csh.CaseID = c.CaseID AndAlso _csh.CaseStatusID = c.CaseStatusID
Select _csh.CaseStatusHistoryID
).Max = csh.CaseStatusHistoryID
Here is what I have so far but it does not return any result - just seems to time out.
IQueryable<CasesViewModel> objs =
from c in _db.Cases
from cs in _db.CaseStatuses.Where(cs => cs.CaseStatusId == c.CaseStatusId.Value).DefaultIfEmpty()
from rg in _db.RepairingGarages.Where(rg => rg.RepairingGarageId == c.RepairingGarageId.Value).DefaultIfEmpty()
from rgb in _db.Businesses.Where(rgb => rgb.BusinessId == rg.BusinessId.Value).DefaultIfEmpty()
from ec in _db.EngineeringCompanies.Where(ec => ec.EngineeringCompanyId == c.EngineeringCompanyId.Value).DefaultIfEmpty()
from ecb in _db.Businesses.Where(ecb => ecb.BusinessId == ec.BusinessId.Value).DefaultIfEmpty()
from csh in _db.CaseStatusHistory.Where(csh => csh.CaseStatusHistoryId ==
(
from _csh in _db.CaseStatusHistory
where _csh.CaseId == c.CaseId && _csh.CaseStatusId == c.CaseStatusId.Value
select _csh.CaseStatusHistoryId
).Max()).DefaultIfEmpty()
select new CasesViewModel
{
CaseID = c.CaseId,
RepairingGarage = rgb.BusinessName,
Engineer = ecb.BusinessName,
CaseStatus = cs.CaseStatus,
StatusChangeDate = csh.DateTimeAdded
};
I just need to get the StatusChangeDate which is the DateTimeAdded in the CaseStatusHistory table. Cases can be at the same status more than once, so I just need the DateTimeAdded with the highest CaseStatusHistoryID for a particular Case. Thank you very much for any help.
This appears to answer my question
IQueryable<CasesViewModel> objs =
from c in _db.Cases
from cs in _db.CaseStatuses.Where(cs => cs.CaseStatusId == c.CaseStatusId.Value).DefaultIfEmpty()
from rg in _db.RepairingGarages.Where(rg => rg.RepairingGarageId == c.RepairingGarageId.Value).DefaultIfEmpty()
from rgb in _db.Businesses.Where(rgb => rgb.BusinessId == rg.BusinessId.Value).DefaultIfEmpty()
from ec in _db.EngineeringCompanies.Where(ec => ec.EngineeringCompanyId == c.EngineeringCompanyId.Value).DefaultIfEmpty()
from ecb in _db.Businesses.Where(ecb => ecb.BusinessId == ec.BusinessId.Value).DefaultIfEmpty()
select new CasesViewModel
{
CaseID = c.CaseId,
RepairingGarage = rgb.BusinessName,
Engineer = ecb.BusinessName,
CaseStatus = cs.CaseStatus,
StatusChangeDate =
(
from _csh in _db.CaseStatusHistory
where _csh.CaseId == c.CaseId && _csh.CaseStatusId == cs.CaseStatusId
orderby _csh.CaseStatusHistoryId descending
select _csh.DateTimeAdded
).FirstOrDefault()
};
The only trouble with not joining is that I am not sure if I can sort by this date now. Any better ideas? Thank you.

LINQ Aggregate results with Many to Many Relationship

I am currently working with this schema
This is how my LINQ currently looks
var regionResults = (
from p in _context.Projects
from pr in p.Regions
where (data.RegionId == null || pr.RegionId == data.RegionId)
group p by pr.RegionId into g
join q in _context.Regions on g.Key equals _context.Regions.First().Id
select new Models.ViewModels.ProjectBreakdownViewModel.Regions
{
RegionName = q.Name,
TotalCount = g.Count(),
RejectedCount = g.Count(e => e.SubmissionStatusId == 2),
DeniedCount = g.Count(e => e.SubmissionStatusId == 3)
});
this is what it is currently producing, albeit incorrect
This is what I need it to be...
I know the problem is with this line, essentially
join q in _context.Regions on g.Key equals _context.Regions.First().Id
I don't know how to do this without the use of .First(), there doesn't seem to be a way to do it. I'm close I just don't know how to finish this.
If you have an collection of ProjectRegions in you Region entity, you can do this:
var result= context.Regions
.Where(r=> data.RegionId == null || r.Id == data.RegionId)
.Select(r=> new
{
RegionName = r.Name,
TotalCount = r.ProjectRegions.Count(),
RejectedCount = r.ProjectRegions.Count(e => e.Project.SubmissionStatusId == 2),
DeniedCount = r.ProjectRegions.Count(e => e.Project.SubmissionStatusId == 3)
});
ProjectRegion entity should have two nav properties, Project and Region, use them to navigate and create the corresponding conditions

Single link query where clause to take only `n` of each type from a single model/table

Having a table Entities with a column type (between other columns) how can I select only n entities from type A and m entities from type b in a single linq query where clause? Is it possible?
I am looking for something like this:
var x = from s in db.Entities
where s.type == `A` && (????) < n
|| s.type == `B` && (????) < m
select s
Current solution with combined queries:
var x = entities.Where(e => e.type == `A`).Take(n).Union(
entities.Where(e => e.type == `B`).Take(m));
You could use GroupBy:
var x = db.Entities.GroupBy(x => x.type)
.Where(g => g.Key == "A" || g.Key == "B")
.SelectMany(g => g.Key == "A" ? g.Take(n) : g.Take(m));
But I don't know why you don't like your Union-based solution - it should result in just one query sent to the database anyway.

Hide Join if string.IsNullOrEmpty using Linq

I got this linq query which searches for selected values in my database using dropdowns.
Is there a way to hide the "join" in the linq query if the ddlCategory is null? I want this because the result of the search shows duplicated-rows because my documents can have many Categories.?? hope you understand what i mean.. Can anyone help??
var documents = from d in data.tblDocuments
join sc in data.tblSubCategories on d.DocId equals sc.DocId
orderby d.Docyear descending
where
(string.IsNullOrEmpty(person) || d.DocPerson.Equals(person)) &&
(string.IsNullOrEmpty(year) || d.Docyear.Equals(year)) &&
(string.IsNullOrEmpty(law) || d.DocLaw.Equals(law)) &&
(string.IsNullOrEmpty(court) || d.DocCourt.Equals(court)) &&
(string.IsNullOrEmpty(category) || sc.CategoryId.Equals(category)) &&
(string.IsNullOrEmpty(casenr) || d.DocNr.Equals(casenr))
select d;
Use lambda syntax:
var query = data.tblDocuments;
if (condition) // conditionally add join
query = query.Join(data.tblSubCategories.Where(sc => sc.CategoryId == category),
d => d.DocId, sc => sc.DocId, (d,sc) => d);
// continue to compose query
query = query.OrderByDescending(d => d.Docyear)
.Where(d => ...);
BTW you can compose filtering based on conditions:
if (!String.IsNullOrEmpty(person))
query = query.Where(d => d.DocPerson == person);

Aggregate functions with a left outer join in LINQ to Entities

I've been looking through related LINQ questions here trying to figure this one out, but I'm having some trouble converting a SQL query of mine to the equivalent LINQ to Entities version.
select companies.CommpanyName,
job.Position,
count(offers.jobID) As Offered,
job.Openings,
job.Filled
from jobs
left outer join offers on jobs.ID = offers.JobID
join membership.dbo.individuals on jobs.UserID = individuals.ID
join membership.dbo.companies on individuals.CompanyID = companies.ID
where jobs.Hidden = 0
group by offers.JobID,
companies.CommpanyName,
job.Position,
job.Openings,
job.Filled
I've done left outer joins in LINQ before similar to this example but I'm not sure how to combine the count and group statements with this to get the desired result:
CompanyName Position Offered Openings Filled
1 Exmaple Co. Job X 0 2 0
2 Example Co. Job Y 4 6 3
3 Test Co. Job Z 1 1 1
The query is further complicated by the fact that it needs to utilize two separate data contexts. I apologize for the lack of example code, but I'm really not sure how to start this, my LINQ-fu is still weak.
Update:
This is the solution I arrived at with Craig's help, had to use LINQ to Objects because of the unfortunate multiple context setup, JobWithOfferCounts is not an entity object:
IEnumerable<Job> t = context1.JobSet.Include("Offers").Include("Contacts").Where(j => j.Hidden == false);
IEnumerable <JobWithOfferCounts> r = (from j in t
join i in context2.IndividualSet on j.UserID equals i.ID
join c in context2.CompanySet on i.CompanyID equals c.ID
select new JobWithOfferCounts()
{
JobObject = j,
CompanyID = Convert.ToInt32(c.ID),
CompanyName = c.HostName,
OfferCount = j.offers.Count(o => o.Rejected == false),
FilledCount = j.offers.Count(o => o.Accepted == true),
PendingCount = j.offers.Count(o => o.Accepted == false && o.Rejected == false)
});
return r;
I can't see why you have individuals in your query, or why you group by offers.JobID when it (unlike jobs.JobId) could be null, but here's a first stab:
var q = from c in Context.Companies
from i in c.Individuals
from j in i.Jobs
where j.Hidden == 0
select new
{
CompanyName = c.CompanyName,
Position = j.Position,
Offered = j.Offers.Count(),
Openings = j.Openings,
Filled = j.Filled
};
It's rarely correct to use join in LINQ to Entities or LINQ to SQL.

Resources