问题
I have the following dilemma :
We have two tables : Patient and Charges , patOID being foreign key within the Charges table.
A simple join between 2 tables:
from itemPat in listPat
join itemCharge in listCharge on itemPat.patOID equals itemC.patOID
select new
{
itemPat.patOID,
itemCharge.paid
}
Considering the face that a patient can have more than 1 charges, how can I get the itemCharge for the biggest value of a certain field( ex. paid ) .
What have i tried :
from itemPat in listPat
join itemCharge in listCharge on itemPat.patOID equals itemC.patOID into zzz
select new
{
itemPat.patOID,
MaxPay=zzz.Max(p=>p.paid),
}
This solution offers me the value of that field but does not give me access to other fields within the record I would like to reffer to . How can I do that ?
Im thinking of something like :
from itemPat in listPat
join itemCharge in listCharge.Where(p=>Max(p.paid)) on itemPat.patOID equals itemC.patOID into zzz
select new
{
itemPat.patOID,
MaxPay=zzz.Max(p=>p.paid),
}
回答1:
from itemPat in listPat
join itemCharge in listCharge on itemPat.patOID equals itemC.patOID into g
select new {
itemPat.patOID,
MaxCharge = g.OrderByDescending(p => p.paid).FirstOrDefault()
}
MaxCharge
property will contain charge object with max paid value, or null if there is no charges for patient.
来源:https://stackoverflow.com/questions/14219129/max-value-in-linq-select-within-innerjoin