Max value in Linq select Within Innerjoin

旧巷老猫 提交于 2019-12-24 18:13:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!