Linq中的最大值选择Innerjoin中 [英] Max value in Linq select Within Innerjoin

查看:220
本文介绍了Linq中的最大值选择Innerjoin中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下困境:

我们有两个表:Patient和Charges,patOID是Charges表中的外键.

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
      }

考虑到患者可以收取1笔以上的费用,我如何才能获得某字段的最大价值的itemCharge(例如,已付费).

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 ) .

我尝试了什么:

 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 ?

我在想类似的东西:

  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),
           }

推荐答案

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属性将包含具有最大已付款值的费用对象,如果不向患者收取费用,则为null.

MaxCharge property will contain charge object with max paid value, or null if there is no charges for patient.

这篇关于Linq中的最大值选择Innerjoin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆