在Linq分组和计数

时间:2013-08-26 04:27:42

标签: c# linq count group-by

我有这个查询,一切正常,除了Count,它返回lstISSHDR中所有记录的计数。我希望COUNT在lstISSHDR中返回field4的计数,其中lstrej.issuergroupseq = lstisshdr.issuergroupseq

var tmpRejSTM = (from r in lstRej
 join l in lstISSHDR on r.IssuerGroupSequence equals l.IssuerGroupSequence
 join s in lstSECHDR on r.SecurityGroupSequence equals s.SecurityGroupSequence
 where r.Field1 == "INVHDR" && !string.IsNullOrEmpty(l.Field4)
 select new { IssuerCode = r.Field4, IssuerName = l.Field6, 
 SecurityCode = s.Field4, CountofIssuerCode = l.Field4.Count() })
.GroupBy(x => new { x.IssuerCode, x.IssuerName, x.SecurityCode, 
 x.CountofIssuerCode })
.OrderBy(x => x.Key.IssuerCode).ThenBy(x => x.Key.CountofIssuerCode).ToList();     

1 个答案:

答案 0 :(得分:0)

我应该改变这个:

CountofIssuerCode = l.Field4.Count()

为:

CountofIssuerCode = lstISSHDR.Where(x=>x.IssuerGroupSequence == l.IssuerGroupSequence).Select(x=>x.Field4).Count()