Simplify many LINQ Join - c#

I have a rather complex LINQ query but that is the point of the question
var result = await _context.TblBreakpoints
.GroupBy(b => new { b.BpgroupId, b.ResultType, b.DrugId, b.Susceptible, b.LowIntermediate, b.Intermediate, b.Resistant})
.Join(_context.TblBreakpointgroups,
bg => bg.Key.BpgroupId,
g => g.BpgroupId,
(bg, g) => new
{
GroupId = bg.Key.BpgroupId,
DrugId = bg.Key.DrugId,
Susceptible = bg.Key.Susceptible,
LowIntermediate = bg.Key.LowIntermediate,
Intermediate = bg.Key.Intermediate,
Method = bg.Key.ResultType,
Resistant = bg.Key.Resistant,
StandardId = g.BpstandardId,
GroupName = g.BpgroupName,
Count = bg.Count(),
})
.Join(_context.TblBreakpointStandards,
i => i.StandardId,
j => j.BpstandardId,
(i, j) => new
{
Standard = j.Bpstandard,
GroupId = i.GroupId,
GroupName = i.GroupName,
Count = i.Count,
Method = i.Method,
DrugId = i.DrugId,
Susceptible = i.Susceptible,
LowIntermediate = i.LowIntermediate,
Intermediate = i.Intermediate,
Resistant = i.Resistant
})
.Join(_context.TblDrugs,
i => i.DrugId,
j => j.DrugId,
(i, j) => new
{
DrugName = j.DrugName,
Standard = i.Standard,
GroupId = i.GroupId,
GroupName = i.GroupName,
Count = i.Count,
Method = i.Method,
Susceptible = i.Susceptible,
LowIntermediate = i.LowIntermediate,
Intermediate = i.Intermediate,
Resistant = i.Resistant
})
.Join(_context.TblBreakpointgroupmembers,
i => i.GroupId,
j => j.BpgroupId,
(i, j) => new
{
OrganismId = j.OrganismId,
Standard = i.Standard,
GroupId = i.GroupId,
GroupName = i.GroupName,
Count = i.Count,
Method = i.Method,
DrugName = i.DrugName,
Susceptible = i.Susceptible,
LowIntermediate = i.LowIntermediate,
Intermediate = i.Intermediate,
Resistant = i.Resistant
})
.Join(_context.TblOrganismNames,
i => i.OrganismId,
j => j.OrganismId,
(i, j) => new BreakpointSummary
{
OrganismName = j.OrganismName,
Standard = i.Standard,
GroupName = i.GroupName,
Count = i.Count,
Method = i.Method,
DrugName = i.DrugName,
Susceptible = i.Susceptible,
LowIntermediate = i.LowIntermediate,
Intermediate = i.Intermediate,
Resistant = i.Resistant
})
.ToListAsync().ConfigureAwait(false);
From the query with each Join I keep passing the previous values and add the value(s) that come from the join. It is already tedious with just 5 joins, it would get even more so with more joins. Is there a better way that I am missing?
I think the equivalent SQL is
WITH bpg (BPGroupId, ResultType, DrugId, Susceptible, LowIntermediate, Intermediate, Resistant, Count)
AS (
SELECT BPGroupId, ResultType, DrugId, Susceptible, LowIntermediate, Intermediate, Resistant, COUNT(BPGroupId)
FROM dbo.tbl_Breakpoint a
GROUP BY BPGroupId,
ResultType,
DrugId,
Susceptible,
LowIntermediate,
Intermediate,
Resistant
)
SELECT a.BpgroupName, b.BPStandard, c.DrugName, e.OrganismName, CTE.ResultType, CTE.Susceptible, CTE.LowIntermediate, CTE.Intermediate, CTE.Resistant, CTE.Count
FROM dbo.tbl_breakpointgroup a
INNER JOIN bpg CTE ON a.BPGroupId = CTE.BPGroupId
INNER JOIN tbl_BreakpointStandard b ON b.BPStandardId = a.BPStandardId
INNER JOIN tbl_Drug c ON c.DrugID = CTE.DrugId
INNER JOIN tbl_breakpointgroupmember d ON d.BPGroupId = CTE.BPGroupId
INNER JOIN tbl_OrganismName e ON e.OrganismId = d.OrganismId
WHERE a.BPGroupId = CTE.BPGroupId

In general when using manual joins in LINQ, it's better to use the query syntax since it provides range variables (which correspond to table/query aliases in SQL) transparency. e.g. (in pseudo code)
from a in queryA
join b in queryB on ... // can use any field from a
join c in queryC on ... // can use any field from a and b
join d in queryD on ... // can use any field from a, b and c
...
select new
{
// can use any field for a, b, c, d etc.
}
The same with method syntax is a bit more complicated, but the principle is to wrap the previous "variables" in simple tuple like anonymous types until you get to the final projection, e.g. (in pseudo code)
queryA
.Join(queryB, a => {a fields}, b => {b fields), (a, b) => new { a, b }) // first result
.Join(queryC, r => {r.a, r.b fields), c => { c fields }, (r, c) => new { r.a, r.b, c } // second result
.Join(queryD, r => {r.a, r.b, r.c fields), d => { d fields }, (r, d) => new { r.a, r.b, r.c, d } // third result
...
.Select(r => new { r.a, r.b, r.c, r.d... fields });
Applying it to your example, the corresponding query syntax could be like (note that sub queries inside can use whatever syntax is appropriate):
var query =
from cte in _context.TblBreakpoints
.GroupBy(b => new { b.BpgroupId, b.ResultType, b.DrugId, b.Susceptible, b.LowIntermediate, b.Intermediate, b.Resistant})
.Select(g => new
{
g.Key.BpgroupId, g.Key.ResultType, g.Key.DrugId, g.Key.Susceptible, h.Key.LowIntermediate, g.Key.Intermediate, g.Key.Resistant,
Count = g.Count(),
})
join a in _context.TblBreakpointgroups on cte.BpgroupId equals a.BpgroupId
join b in _context.TblBreakpointStandards on a.BpstandardId equals b.BpstandardId
join c in _context.TblDrugs on cte.DrugId equals c.DrugId
join d in _context.TblBreakpointgroupmembers on cte.BpgroupId equals d.BpgroupId
join e in _context.TblOrganismNames on d.OrganismId equals e.OrganismId
select new BreakpointSummary
{
OrganismName = e.OrganismName,
Standard = b.Bpstandard,
GroupName = a.BpgroupName,
Count = cte.Count,
Method = cte.ResultType,
DrugName = d.DrugName,
Susceptible = cte.Susceptible,
LowIntermediate = cte.LowIntermediate,
Intermediate = cte.Intermediate,
Resistant = cte.Resistant,
};
You can convert it to method syntax using the aforementioned rules, but for me it doesn't worth the effort.

Related

Pass where condition as parameter using LINQ expression query

Initially I have the following query in a function.
This query is used in the join in another query, which I didn't add due to its size.
var latestGuestRegistration = from c in Context.Guest
group c by c.PersonId
into cc
select new { PersonId = cc.Key, CreationTime = cc.Max(a => a.CreationTime) };
I would like to add a filter, however I could filter by name or email. So I would like to pass this filter as a function parameter.
I couldn't do this using C#'s inline SQL syntax, so I tried converting to LINQ:
var tmp = Context.GuestRegistrations
.Where(filter) // Here I filter by email or name
.GroupBy(x => x.PersonId)
.Select(cc => new { PersonId = cc.Key, CreationTime = cc.Max(a => a.CreationTime) })
.AsQueryable();
According to Rider, both implementations have the same data type, which is Queryable<{ID, CreationTime}>
// Joins above
join lt in tmp on
new { guestRegistration.PersonId, guestRegistration.CreationTime } equals
new { lt.PersonId, lt.CreationTime }
// Joins below
When this join is executed using my LINQ, it seems that it already returns the data, while the original solution enters as a subquery. How can I add a filter to my original query?
In the end, I would like something more or less like this:
Expression<Func<GuestRegistration,bool>> wherePredicate = registration => registration.FullName = "Gabriel";
var latestGuestRegistration = from c in Context.GuestRegistrations
where wherePredicate
group c by c.PersonId
into cc
select new { PersonId = cc.Key, CreationTime = cc.Max(a => a.CreationTime) };
Solution
Thanks to everyone who responded and commented, below is a summary of the final code.
private IQueryable<...> GetSearch(Expression<Func<G, bool>> filter) {
var filteredG= Context.G.Where(filter);
var latestG = from c in filteredG
group c by c.PersonId
into cc
select new { PersonId = cc.Key, CreationTime = cc.Max(a => a.CreationTime) };
var bigQuery = from ... join latestG ...
}
GetSearch(x => x.Fullname == "Gabriel")
Some expressions cannot be injected via Query syntax, but you can mix it with Method Chain syntax:
Expression<Func<GuestRegistration,bool>> wherePredicate = registration => registration.FullName = "Gabriel";
var guestRegistrations = Context.GuestRegistrations
.Where(wherePredicate);
var latestGuestRegistration =
from c in guestRegistrations
group c by c.PersonId
into cc
select new { PersonId = cc.Key, CreationTime = cc.Max(a => a.CreationTime) };

failing in result for left outer join c#

I want to do a left outter join between two query and populate the result in new instance of object
this is the code for first query , which individually works fine, fetches the data
var finances = _context.Finances
.GroupBy(p => p.CustomerID)
.Select(m => new
{
CustomerID = m.Key,
Debtor = m.Sum(p => p.Debtor),
Creditor = m.Sum(p => p.Creditor),
}).ToList();
and second query , which individually works fine, fetches the data
var events = _context.Events
.Include(p=>p.Customer)
.Where(p=>p.Start.ToString().CompareTo(reqDate) >= 0)
.Select(p => new
{
CustomerID = p.CustomerID,
FullName =p.Customer.FirstName + ' ' +p.Customer.LastName,
End =p.End,
Start = p.Start,
}).ToList();
So the left outer join would be like this
var qry = events.GroupJoin(
finances,
ev1 => ev1.CustomerID,
fi1 => fi1.CustomerID,
(f, bs) => new { events = f, finances = bs })
.SelectMany(
eventsFinances => eventsFinances.finances.DefaultIfEmpty(),
(x, y) => new RequestEventsDto
{
CustomerID=x.events.CustomerID,
Start = x.events.Start,
End = x.events.End,
FullName = x.events.FullName,
Debtor = y.Debtor,
Creditor = y.Creditor,
});
the events table would populate the data but the finance table wouldn't ( which actuallu has data inside of it) and gives this error
"Object reference not set to an instance of an object."
if I comment these two lines form finance table ,it works fine the rest
Debtor = y.Debtor,
Creditor = y.Creditor,
which part of the left outer join has a problem?
try this
var var qry = from e in events
join f in finances on e.CustomerID equals f.CustomerID into fj
from f in fj.DefaultIfEmpty()
select new {
CustomerID=e.CustomerID,
Start =e.Start,
End = e.End,
FullName = e.FullName,
Debtor = f.Debtor,
Creditor = f.Creditor
};

How To Join Many Tables In Linq Lambda Expressions?

I Got This Lambda Expression But Does Not Work Correctly.Does Not Return Any Thing.Would You Help me Please on this:
var query = db.Cheque
.Join(db.Contracts,
C => C.ContractIDRef,
Con => Con.ContractID,
(C, Con) => new { Cheques1 = C, Contracts1 = Con })
.Join(db.Parties,
Con => Con.Contracts1.ContractID,
Pt => Pt.ContractIDRef,
(Con, Pt) => new { Contract2 = Con, Parites1 = Pt })
.Join(db.Persons,
Pt => Pt.Parites1.PartyIDRef,
P => P.PersonID,
(Pt, P) => new { Parites2 = Pt, Persons1 = P })
.Join(db.Company,
Pt => Pt.Parites2.Parites1.CompanyIDRef,
Com => Com.CompanyID,
(Pt, Com) => new { Parites3 = Pt, Company1 = Com })
.Join(db.Bank,
C => C.Parites3.Parites2.Contract2.Cheques1.BankIDRef,
B => B.BankID,
(C, B) => new { Cheque2 = C, Bank1 = B })
.Join(db.Flats,
Con => Con.Cheque2.Parites3.Parites2.Contract2.Contracts1.FlatIDRef,
F => F.FlatID,
(Con, F) => new { Contract3 = Con, Flat1 = F })
.Join(db.Projects,
F => F.Flat1.ProjectIDRef,
Pr => Pr.ProjectID,
(F, Pr) =>
new
{
ChequeNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeNo,
ChequeIDRef = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeIDRef,
ChequePrice = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.Amount,
BankName = F.Contract3.Bank1.BankName,
BranchName = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.BranchName,
ChequeDate = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeDate,
AccountNumber = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.AccNo,
AccountOwner = F.Contract3.Cheque2.Parites3.Parites2.Contract2.Cheques1.ChequeOwnerName,
}
)
`.Where(Total => SelectedChequesList.Contains(Total.ChequeIDRef.Value)).ToList();
I will start with converting the above to the query syntax. While I'm a fan of the method syntax, using it in a complex queries involving multiple joins is a real pain. I needed a lot of time to read and try to follow the above query, so let do that:
var query =
from cheque in db.Cheque
join contract in db.Contracts on cheque.ContractIDRef equals contract.ContractID
join party in db.Parties on contract.ContractID equals party.ContractIDRef
join person in db.Persons on party.PartyIDRef equals person.PersonID
join company in db.Companies on party.CompanyIDRef equals company.CompanyID
join bank in db.Bank on cheque.BankIDRef equals bank.BankID
join flat in db.Flats on contract.FlatIDRef equals flat.FlatID
join project in db.Projects on flat.ProjectIDRef equals project.ProjectID
where SelectedChequesList.Contains(cheque.ChequeIDRef.Value)
select new
{
ChequeNumber = cheque.ChequeNo,
ChequeIDRef = cheque.ChequeIDRef,
ChequePrice = cheque.Amount,
BankName = bank.BankName,
BranchName = cheque.BranchName,
ChequeDate = cheque.ChequeDate,
AccountNumber = cheque.AccNo,
AccountOwner = cheque.ChequeOwnerName,
};
Now, one thing that can be seen is that most of the joins are not used. But let assume they are needed for some reason. Note that all the joins are INNER joins, so any of them can cause the query to return empty result if there is no matching record.
The problem is most probably in this join
join person in db.Persons on party.PartyIDRef equals person.PersonID
Instead of PartyIDRef I guess it should be something like PersonIDRef.

SQL query to LINQ conversion with nested select statements

I want to convert the following query to LINQ:
SELECT LV.* FROM LowerVehicles LV
INNER JOIN (Select VSerial,MAX(updatedOn) MaxUpdatedOn from LowerVehicles group by vserial) LVG
ON LV.VSerial = LVG.VSerial AND LV.updatedOn = LVG.MaxUpdatedOn
Not knowing your entities classes, here is an approximation. You can use query syntax or fluent syntax. Sometimes one is preferable over the other, and in the case of joins and grouping I prefer to use query syntax.
QUERY SYNTAX
var query = from LV in LowerVehicles
join LVG in (
from r in LowerVehicles
group r by r.vserial into g
select new {VSerial = g.Key, MaxUpdatedOn = g.Max(t => t.updatedOn)})
on LV.VSerial equals LVG.Vserial
and LV.updatedOn equals LVG.MaxUpdatedOn
select LV;
FLUENT SYNTAX
var lvg = LowerVehicles.GroupBy(t => t.vserial)
.Select(g => new {
VSerial = g.Key,
MaxUpdatedOn = g.Max(t => t.updatedOn)
});
var query = LowerVehicles.Join(
lvg,
a => new { a.VSerial, a.updatedOn },
b => new { b.VSerial, b.MaxUpdatedOn },
(a, b) => new { LV = a, LVG = b}
)
.Select(t=> t.LV);
Something like this?
Something.LowerVehicles
.Join(something.LowerVehicles.Select(y => new { y.VSerial, updatedOn = y.updatedOn.Max() }).GroupBy(z => z.VSerial),
x => new { x.VSerial, x.updatedOn },
lvg => new { lvg.VSerial, lvg.updatedOn },
(x, y) => x)

Left outer join of datatable with linq expression?

I have two DataTables:
DataTable dtFields = new DataTable("tmpFieldTable");
dtFields.Columns.Add("FieldID");
dtFields.Columns.Add("CDGroupID");
dtFields.Columns.Add("CDCaption");
dtFields.Columns.Add("fldIndex");
and
DataTable dtCDGroup = new DataTable("tmpCDGroup");
dtCDGroup.Columns.Add("CDGroupID");
dtCDGroup.Columns.Add("Name");
dtCDGroup.Columns.Add("Priority");
I am using following LINQ expression to join these tables:
var resultList = dtFields.AsEnumerable()
.Join(dtCDGroup.AsEnumerable(),
fieldList => fieldList.Field<string>("CDGroupID"),
cd => cd.Field<string>("CDGroupID"),
(fieldList, cd) => new
{
FieldID = fieldList.Field<string>("FieldID"),
CdGroup = cd.Field<string>("Name"),
CDCaption = fieldList.Field<string>("CDCaption"),
Priority = (cd.Field<string>("Priority") ?? "99"),
fldIndex = fieldList.Field<string>("fldIndex").ToString()
})
.OrderBy(result => result.Priority)
.ThenBy(result => result.fldIndex);
How can I perform left outer join with these DataTables?
Left join are not natively offered using lambda expression. but you can achieve this using GroupJoin and SelectMany method.
var result = new string[] { "A", "B" }.GroupJoin(new string[] { "B" }, l => l, r => r, (l, r) => new { l, r }).ToList();
Console.WriteLine(result.Count());
this will result result[0].r.Count() = 0 and result[1].r.Count() = 1.
Hope this will help.
Regards.
Just add DefaultIfEmpty() to your table:
resultList = dtFields.AsEnumerable()
.Join(dtCDGroup.AsEnumerable().DefaultIfEmpty(),
fieldList => fieldList.Field<string>("CDGroupID"),
cd => cd.Field<string>("CDGroupID"),
(fieldList, cd) => new
{
FieldID = fieldList.Field<string>("FieldID"),
CdGroup = cd.Field<string>("Name"),
CDCaption = fieldList.Field<string>("CDCaption"),
Priority = (cd.Field<string>("Priority") ?? "99"),
fldIndex = fieldList.Field<string>("fldIndex").ToString()
})
.OrderBy(result => result.Priority)
.ThenBy(result => result.fldIndex);

Categories