LINQ Left Join with group by and count - c#

I am having fun with LINQ query and kind of stuck at figuring out the correct method to get Count of associated entries.
I have below LINQ query and
var result = (from OR in orders
join OE in order_entries on OR.id equals OE.order_id into temp
from LOE in temp.DefaultIfEmpty()
group LOE by new {OR.user_id, OR.site } into g
select new {
col1 = g.Key.user_id,
col2 = g.Key.site,
count = g.Count() ,
cost = g.Sum( oe => oe.cost)
}
);
this turns to
SELECT
1 AS [C1],
[GroupBy1].[K1] AS [user_id],
[GroupBy1].[K2] AS [site],
[GroupBy1].[A1] AS [C2],
[GroupBy1].[A2] AS [C3]
FROM ( SELECT
[Extent1].[user_id] AS [K1],
[Extent1].[site] AS [K2],
COUNT(1) AS [A1],
SUM([Extent2].[cost]) AS [A2]
FROM [dbo].[orders] AS [Extent1]
LEFT OUTER JOIN [dbo].[order_entries] AS [Extent2] ON [Extent1].[id] = [Extent2].[order_id]
GROUP BY [Extent1].[user_id], [Extent1].[site]
) AS [GroupBy1]
What I am trying to acheive here is replace Count(1) with Count([Extent2].[id]) so in case when there is no entries associated with the order I want to show 0 instead of 1.
Can someone please help me with updating the LINQ query to achieve this ?
UPDATE :
replace with below will return the outcome for what I wanted but this also turns my sql query to perform slower..
g.Where(i => i.orders != null).Count(),

The simplest way is to use subqueries:
var qry = from o in orders
select new {
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = order_entries.Where(oe=>oe.OrderId == o.ID).Count(),
cost = order_entries.Where(oe=>oe.OrderId == o.ID).Sum(oe=>oe.Cost)
};
But if you would like to join two data sets, use this:
var qry = (from o in orders join oe in order_entries on o.ID equals oe.OrderId into grp
from g in grp.DefaultIfEmpty()
select new{
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = grp.Count(),
cost = grp.Sum(e=>e.Cost)
}).Distinct();
I strongly believe in it that second query could be writen in simplest way by using group statement.
Here is a complete LinqPad sample:
void Main()
{
List<TOrder> orders = new List<TOrder>{
new TOrder(1, 1, "Site1"),
new TOrder(2, 1, "Site1"),
new TOrder(3, 2, "Site2"),
new TOrder(4, 2, "Site2"),
new TOrder(5, 3, "Site3")
};
List<TOrderEntry> order_entries = new List<TOrderEntry>{
new TOrderEntry(1, 1, 5.5),
new TOrderEntry(2, 1, 6.2),
new TOrderEntry(3, 1, 4.9),
new TOrderEntry(4, 1, 55.15),
new TOrderEntry(5, 1, 0.97),
new TOrderEntry(6, 2, 2.23),
new TOrderEntry(7, 2, 95.44),
new TOrderEntry(8, 2, 3.88),
new TOrderEntry(9, 2, 7.77),
new TOrderEntry(10, 3, 25.23),
new TOrderEntry(11, 3, 31.13),
new TOrderEntry(12, 4, 41.14)
};
// var qry = from o in orders
// select new {
// oid = o.ID,
// uid = o.UserId,
// site = o.Site,
// count = order_entries.Where(oe=>oe.OrderId == o.ID).Count(),
// cost = order_entries.Where(oe=>oe.OrderId == o.ID).Sum(oe=>oe.Cost)
// };
// qry.Dump();
var qry = (from o in orders join oe in order_entries on o.ID equals oe.OrderId into grp
from g in grp.DefaultIfEmpty()
//group g by g into ggg
select new{
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = grp.Count(),
cost = grp.Sum(e=>e.Cost)
}).Distinct();
qry.Dump();
}
// Define other methods and classes here
class TOrder
{
private int iid =0;
private int uid =0;
private string ssite=string.Empty;
public TOrder(int _id, int _uid, string _site)
{
iid = _id;
uid = _uid;
ssite = _site;
}
public int ID
{
get{return iid;}
set{iid = value;}
}
public int UserId
{
get{return uid;}
set{uid = value;}
}
public string Site
{
get{return ssite;}
set{ssite = value;}
}
}
class TOrderEntry
{
private int iid = 0;
private int oid = 0;
private double dcost = .0;
public TOrderEntry(int _iid, int _oid, double _cost)
{
iid = _iid;
oid = _oid;
dcost = _cost;
}
public int EntryId
{
get{return iid;}
set{iid = value;}
}
public int OrderId
{
get{return oid;}
set{oid = value;}
}
public double Cost
{
get{return dcost;}
set{dcost = value;}
}
}

Related

SQL to LINQ handling Match in Left outer JOIN

I am trying to convert below SQL to LINQ query and I am stuck with filtering results using the Match in Left outer Join.
SELECT #batchID , IQ1.ID, #environment , 'IRD', IQ1.ReportingDate, IQ1.Match
FROM (
SELECT TD.*, RD.Match
FROM TransactionDetail TD
INNER JOIN .dbo.Measure M ON M.InternalID = TD.MetricCode-- and TD.BatchID = 'e07f9855-b286-4406-9189-5cfb2a7914c8'
LEFT OUTER JOIN (
SELECT tmp.ID, tmp.ReportingDate, 1 AS Match
FROM tmp
) AS RD ON RD.ID = M.Frequency AND RD.ReportingDate = TD.ReportingDate
WHERE RD.Match IS NULL AND
TD.BatchID = #batchID AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'NRD') AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'RDP') AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'RDF')
) AS IQ1
I have so far achieved below,
// Prepare data for left outer join
var rd = (from tt in result
select new { ID = tt.Id, tt.ReportingDate });
// inner join
var td = TransactionDetail.Join(
MesureTb,
t => t.MetricCode,
m => m.InternalId,
(t, m) => new
{
t.Id,
t.RowAction,
t.BatchId,
t.TrustCode,
t.MetricCode,
t.ReportingDate,
t.Value,
t.UpperBenchmark,
t.LowerBenchmark,
m.InternalId,
Frequency = m.Frequency
});
// left outer join
var failureTransactionDetail = (from p in td
join q in rd on new { ReportingDate = (DateTime)p.ReportingDate, ID = p.Frequency } equals new { q.ReportingDate, q.ID }
into LJ
//select new { p.Id, p.BatchId, p.ReportingDate, RD = q.ReportingDate, q.ID, p.Frequency });
from value in LJ.DefaultIfEmpty()
//where p.BatchId == batchId
select new {p.BatchId, p.Id, Match = 1, p.ReportingDate } into DJ
// LEFT OUTER JOIN
where DJ.BatchId == batchId
////&& DJ.Match == 0
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "NRD"))
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "RDP"))
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "RDF"))
select new { DJ.Id, DJ.ReportingDate, DJ.BatchId } );
My question being how i can achieve similar result as 1 AS Match does in SQL in Linq.
Could someone please guide me? Currently the SQL query returns 2 results based on Match value as null, but the LInq returns 8 results since it is not filtering the Match on Left join.
Any help is much appreciated.
Thanks in advance.
I'm just taking a stab at helping you out here because the question is a little unclear. But just comparing your sql statement to your linq query I can see that you may be trying to filter where: RD.Match IS NULL? If that assumption is correct then there is problem with your linq query.
Given the following objects:
public class TransactionDetail
{
public TransactionDetail(int id,
int batchId,
int metricCode,
DateTime reportingDate)
{
Id = id;
BatchId = batchId;
MetricCode = metricCode;
ReportingDate = reportingDate;
}
public int Id { get; }
public int BatchId { get; }
public int MetricCode { get; }
public DateTime ReportingDate { get; }
}
public class Measure
{
public Measure(int internalId,
int frequency)
{
InternalId = internalId;
Frequency = frequency;
}
public int InternalId { get; }
public int Frequency { get; }
}
public class Tmp
{
public Tmp(int id,
DateTime reportingDate)
{
Id = id;
ReportingDate = reportingDate;
}
public int Id { get; }
public DateTime ReportingDate { get; }
}
Sample Code:
static void Main(string[] args)
{
var transactionDetails = new List<TransactionDetail>
{
new TransactionDetail(id: 1, batchId: 1, metricCode: 1, reportingDate: new DateTime(2019, 1, 1)),
new TransactionDetail(id: 2, batchId: 1, metricCode: 2, reportingDate: new DateTime(2019, 1, 1))
};
var matches = new List<Measure>
{
new Measure(internalId: 1, frequency: 1),
new Measure(internalId: 2, frequency: 3)
};
var temporaryList = new List<Tmp>
{
new Tmp(1, new DateTime(2019, 1, 1)),
};
var transDetails = transactionDetails.Join(
matches,
t => t.MetricCode,
m => m.InternalId,
(t, m) => new
{
t.Id,
t.BatchId,
t.MetricCode,
t.ReportingDate,
m.InternalId,
m.Frequency
})
.ToList();
var failureTransactionDetail = transDetails
.GroupJoin(temporaryList,
trandetail => new { trandetail.ReportingDate, Id = trandetail.Frequency },
tmp => new { tmp.ReportingDate, tmp.Id },
(trandetail, tmp) => new { trandetail, tmp })
.SelectMany(t => t.tmp.DefaultIfEmpty(), (t, value) => new { t, value, Matches = 1 })
.Where(arg => !arg.t.tmp.Any());
Console.WriteLine(JsonConvert.SerializeObject(failureTransactionDetail, Formatting.Indented));
Console.ReadLine();
}
Examine the output and you'll see that you don't need Match = 1. .Where(arg => !arg.t.tmp.Any()) would be the equivalent to RD.Match IS NULL in your sql query.
Hope that puts you in the right direction.

Get the latest record in join linq c#

I have this query
return from fitUpExaminationDetails in _ctx.FitUpExaminationDetails
where fitUpExaminationDetails.FitUpExaminationId == Id
join joint in _ctx.Joints on fitUpExaminationDetails.JointId equals joint.Id
join line in _ctx.Lines on joint.LineId equals line.Id
join fileIsoManager in _ctx.FileIsoManagers on line.Id equals fileIsoManager.LineId
select new ViewDomainClass.QualityControl.Report.ViewFitupExaminationReport
{
HeatNumber1 = fitUpExaminationDetails.HeatNumber1,
HeatNumber2 = fitUpExaminationDetails.HeatNumber2,
JointNumber = joint.JointNumber,
LineNumber = line.LineNumber,
Revision = fileIsoManager.Revision,
};
My line tables can have multi fileIsoManager records.So in the select statement i mean in this part Revision = fileIsoManager.Revision i want to show the latest record in fileIsoManager table .how can i change this query to do that ?
Ok, since you didn't provide much details, i've wrote a few classes to show you how to get your latest revision for each line. Pay attention to the "main" function.
// These are a representation of your classes :
public class Line
{
public int LineId { get; set; }
public string SomeValue { get; set;}
}
public class Revision
{
public int RevisionId { get; set; }
public int LineId { get; set;}
}
void Main()
{
// generating some data so we can test the query.
var lines = new List<Line>() {
new Line() { LineId = 1, SomeValue = "Allo!" }
};
var revisions = new List<Revision>() {
new Revision() { LineId = 1, RevisionId = 1 },
new Revision() { LineId = 1, RevisionId = 2 },
new Revision() { LineId = 1, RevisionId = 3 }
};
var result = (
from line in lines
join revision in revisions on line.LineId equals revision.LineId
group revision by line into grp
select new
{
Line = grp.Key,
LastRevision = grp.OrderByDescending(rev => rev.RevisionId).FirstOrDefault()
}
).ToList();
}
So, basically, you need to group your revisions by line. In the select part, you can then get the latest revision for each line.
In this example, result will contain a dynamic object containing the line id=1 and the revision id = 3.
Going back to your query, it should look like this to get a similar result :
return
from fitUpExaminationDetails in _ctx.FitUpExaminationDetails
where fitUpExaminationDetails.FitUpExaminationId == Id
join joint in _ctx.Joints on fitUpExaminationDetails.JointId equals joint.Id
join line in _ctx.Lines on joint.LineId equals line.Id
join fileIsoManager in _ctx.FileIsoManagers on line.Id equals fileIsoManager.LineId
group fileIsoManager by new { fitUpExaminationDetails, joint, line} into grp
select new ViewDomainClass.QualityControl.Report.ViewFitupExaminationReport
{
HeatNumber1 = grp.Key.fitUpExaminationDetails.HeatNumber1,
HeatNumber2 = grp.Key.fitUpExaminationDetails.HeatNumber2,
JointNumber = grp.Key.joint.JointNumber,
LineNumber = grp.Key.line.LineNumber,
Revision = grp.OrderByDescending(fileIsoMgr => gileIsoMgr.Id).FirstOrDefault()
};

Joint 3 tables in linq and return value of nested table with max id

I have there tables as you can see :
line:
id Linename
1 line1
2 line2
3 line3
joint:
id lineId jointname
1 1 joint1
2 2 joint2
3 1 joint3
fitup:
id jointid fitupdate state
1 1 2012/12/12 acc
2 1 2013/12/12 rej
3 2 2015/12/12 acc
4 2 2016/12/12 rej
Result i need:
id Linename jointname fitupdate state
1 line1 joint1 2013/12/12 rej
2 line2 joint2 2016/12/12 rej
The fitup table has a state I need the final state based on max id.
In the fitup table i have multi rows for each joint but i need the date(string) of max id in the result query .
Here is my query:
var q = from j in _ctx.Joints
join l in _ctx.Lines on j.LineId equals l.Id
join spo in _ctx.Spools on j.SpoolId equals spo.Id
join sup in _ctx.Supports on j.SupportId equals sup.Id
join shee in _ctx.Sheets on j.SheetId equals shee.Id
join Fit in _ctx.FitUpDetails on j.Id equals Fit.JointId into g2
from y2 in g2.DefaultIfEmpty()
join weld in _ctx.WeldDetails on j.Id equals weld.JointId into g
from y1 in g.DefaultIfEmpty()
join end in _ctx.Ends on j.EndId equals end.Id
join basemat in _ctx.BaseMaterials on j.BaseMaterialId equals basemat.Id
join TestPack in _ctx.TestPackages on j.TestPackageId equals TestPack.Id
group new { j, l,y2,y1} by new { shee, j, l, spo, sup, y2, y1, end, basemat, TestPack } into grouping
let maxFitById = grouping.Select(item => item.y2)
.Where(item => item != null)
.OrderByDescending(item => item.Id)
let maxweldById = grouping.Select(item => item.y1)
.Where(item => item != null)
.OrderByDescending(item => item.Id)
select new ViewFront()
{
Id = grouping.Key.j.Id,
LineId = grouping.Key.l.LineNumber,
SubmitDateTime = grouping.Key.j.SubmitDateTime,
JointNumber = grouping.Key.j.JointNumber,
BaseMaterialId = grouping.Key.basemat.Name,
FitUpAccept = maxFitById.FirstOrDefault().StateStep1,
FitUpAcceptMain = maxFitById.FirstOrDefault().StateStep2,
JointClass = grouping.Key.j.JointClass,
End = grouping.Key.end.Name,
JointSize = grouping.Key.j.JointSize,
LeftMaterialItemCode = grouping.Key.j.LeftMaterialItemCode,
LeftMaterialLength = grouping.Key.j.LeftMaterialLength.ToString(),
MagneticTest = grouping.Key.j.MagneticTest,
PenetrationTest = grouping.Key.j.PenetrationTest,
PostWeldHeatTreatment = grouping.Key.j.PostWeldHeatTreatment,
RemarkState = grouping.Key.j.RemarkState,
RightMaterialItemCode = grouping.Key.j.RightMaterialItemCode,
RightMaterialLength = grouping.Key.j.RightMaterialLength.ToString(),
RadiographyTest = grouping.Key.j.RadiographyTest,
SheetId = grouping.Key.shee.SheetNumber,
ShopField = grouping.Key.j.ShopField,
SpoolId = grouping.Key.spo.SpoolNumber,
SupportId = grouping.Key.sup.SupportNumber,
TestPackageId = grouping.Key.TestPack.PackageNumber,
THK = grouping.Key.j.THK,
UltrasonicTest = grouping.Key.j.UltrasonicTest,
WeldAccept = maxweldById.FirstOrDefault().StateStep1,
WeldAcceptMain = maxweldById.FirstOrDefault().StateStep2
};
In this query FitUpAccept is the state .
Joint table data
weld :
fitup:
result:
The following code does what you need. Now some explanations:
I kept only the tables relevant for the described output data just to keep it simpler.
When grouping by - If you select the entire objects as you did then you will always get "groups" of a single record - I group the wanted data just by the key - In this case the non aggregated fields. Make sure that you are not grouping by the same fields that you want to actually do aggregation operations by them ( like y2 )
Because it is a left join to the FitUpDetails I must make sure that I remove all the null records and whenever I access a property of that object to make sure it is not null - c# 6.0 syntax of ?..
Now for the By max id part - if I put it into words: "Grouping the data by X, and then for each group ordering it by Y, take first record -> its properties"
So to the code:
var result = (from j in Joints
join l in Lines on j.LineId equals l.Id
join f in FitUpDetails on j.Id equals f.JointId into g2
from y2 in g2.DefaultIfEmpty()
group new { j, l, y2 } by new { j.Id, l.LineName, j.JointName } into grouping
let maxFitById = grouping.Select(item => item.y2)
.Where(item => item != null)
.OrderByDescending(item => item.Id)
.FirstOrDefault()
select new
{
Id = grouping.Key.Id,
LineName = grouping.Key.LineName,
JointName = grouping.Key.JointName,
FitUpdate = maxFitById?.FitUpdate,
State = maxFitById?.State
}).ToList();
Used this for testing it:
List<dynamic> Joints = new List<dynamic>
{
new { Id = 1, LineId = 1, JointName = "joint1" },
new { Id = 2, LineId = 2, JointName = "joint2" },
new { Id = 3, LineId = 1, JointName = "joint3" },
};
List<dynamic> Lines = new List<dynamic>
{
new { Id = 1, LineName = "line1" },
new { Id = 2, LineName = "line2" },
new { Id = 3, LineName = "line3" },
};
List<dynamic> FitUpDetails = new List<dynamic>
{
new { Id = 1, JointId = 1, FitUpdate = new DateTime(2012,12,12), State = "acc" },
new { Id = 2, JointId = 1, FitUpdate = new DateTime(2013,12,12), State = "rej" },
new { Id = 1, JointId = 2, FitUpdate = new DateTime(2015,12,12), State = "acc" },
new { Id = 4, JointId = 2, FitUpdate = new DateTime(2016,12,12), State = "rej" },
};

update with LINQ, and in operator select

Im tryig to transform a SP, into a linq query
This is my SP right now :
UPDATE PatientAlertsSummary
SET IsViewed =1
WHERE PatientID IN (SELECT PatientID FROM PatientTherapist WHERE TherapistID=#TherapistID)
I tried to make a query linq code and this is (so far) my linq query:
I came across some difficulty with doing this
var query = from pas in context.PatientAlertsSummaries
where pas.PatientID.Contains(from pt in context.PatientTherapists where pt.TherapistID == therapistid )
select pas;
foreach (var item in query)
{
item.IsViewed = true;
}
how can I make it right ?
You could change the query to join on the tables to retrieve the results you want, then update the records accordingly:
var query = from pas in context.PatientAlertsSummaries
join pt in context.PatientTherapists on pas.PatientID equals pt.PatientID
where pt.TherapistID == therapistid
select pas;
foreach (var item in query)
{
item.IsViewed = true;
}
Obviously this is untested and based on your current sql / linq query.
You can do something like this:
public class PatientAlertsSummary
{
public bool IsViewed;
public int PatientID;
}
public class PatientTherapist
{
public int PatientID;
public int TherapistID;
}
public void UpdateIsViewed(PatientAlertsSummary summary)
{
summary.IsViewed = true;
}
[TestMethod]
public void Test1()
{
//UPDATE PatientAlertsSummary
//SET IsViewed =1
//WHERE PatientID IN (SELECT PatientID FROM PatientTherapist WHERE TherapistID=#TherapistID)
var therapistId = 1;
var patientAlertsSummaries = new List<PatientAlertsSummary>() {
new PatientAlertsSummary(){PatientID = 1},
new PatientAlertsSummary(){PatientID = 2},
new PatientAlertsSummary(){PatientID = 3}
};
var PatientTherapists = new List<PatientTherapist>() {
new PatientTherapist(){PatientID = 1 , TherapistID = 1},
new PatientTherapist(){PatientID = 2 , TherapistID = 1},
new PatientTherapist(){PatientID = 3, TherapistID = 2}
};
var updatedPatinets1 = PatientTherapists.
Where(o => o.TherapistID == therapistId).
Join(patientAlertsSummaries,
patientTherapist => patientTherapist.PatientID,
patientAlertsSummary => patientAlertsSummary.PatientID,
(o, p) =>
{
UpdateIsViewed(p);
return p;
}).ToList();
}

Use the Linq Count

I want to show OrderDetails Count near the orders information in the grid but in the select Unit i can only select the Key and Count. What is the way to select the orders information?
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data.OrderID into grouped
select new
{
grouped=g.Key,
Count = grouped.Count()
};
You can group it by whole order entity like
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data into grouped
select new
{
OrderId = grouped.Key.OrderId,
OrderDate = grouped.Key.OrderDate
Shipping = grouped.Key.Shipping
.
.
.
Count = grouped.Count()
};
EDIT Linqpad program for similar query on in memory collection of objects
void Main()
{
var orders = new List<Order>{
new Order{OrderId = 1, DeliverIn = 5},
new Order{OrderId = 2, DeliverIn = 6},
new Order{OrderId = 3, DeliverIn = 5},
};
var lines = new List<OrderLine>{
new OrderLine{LineId = 1, OrderId = 1, ProductId = 1},
new OrderLine{LineId = 2, OrderId = 1, ProductId = 2},
new OrderLine{LineId = 3, OrderId = 1, ProductId = 3},
new OrderLine{LineId = 4, OrderId = 2, ProductId = 1},
new OrderLine{LineId = 5, OrderId = 2, ProductId = 3},
new OrderLine{LineId = 6, OrderId = 2, ProductId = 4},
};
var query = from o in orders join l in lines on
o.OrderId equals l.OrderId
group o by o into grouped
select new
{
Count = grouped.Count(),
grouped.Key.OrderId,
grouped.Key.DeliverIn
};
Console.WriteLine(query);
}
// Define other methods and classes here
public class Order
{
public int OrderId{get;set;}
public int DeliverIn{get;set;}
}
public class OrderLine
{
public int LineId{get;set;}
public int OrderId{get;set;}
public int ProductId{get;set;}
}
and if you don't have linq pad simply go and grab it from their site. It is simply awesome.
Check out IGrouping documentation on MSDN.
public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>,
IEnumerable
Pay attention to IEnumerable. Count is just an extension method of IEnumerable. You can easily Select from grouping or loop through it.
For example:
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data.OrderID into grouped
select new
{
grouped=g.Key,
Count = grouped.Count(),
Orders = grouped.ToArray()
//you can also just return grouped itself to support lazy queries
};
Just flatten them into array or list and then get its count.
select new
{
Key = g.Key,
Orders = grouped.ToArray()
};
Then get count:
int count = result.Orders.Count; // Property of an array.

Categories

Resources