Linq集团计数项目

时间:2011-12-05 17:47:29

标签: vb.net linq linq-to-entities

我有这个linq到实体

From r In ReceiptRepository.Fetch
Where 
  r.RECEIPTDATE >= ReportStartDate And
  r.RECEIPTDATE <= ReportEndDate
From p In r.RECEIPTPAYMENTs
Group p By Tender = New With 
  {
     .TenderType = p.PAYMENTTYPE.TENDERTYPE, 
     .TenderName = p.PAYMENTTYPE.TENDERNAME
  } Into Group
Select New SalesTotalCount() With 
  {
     .Id = Tender.TenderType, 
     .Name = Tender.TenderName, 
     .Total = Group.Sum(Function(a) a.AMOUNT), 
     .Count = Group.Count
  }

这是正常工作,除了count属性,它只是给出组计数。我不知道如何找出每个投标计数

1 个答案:

答案 0 :(得分:1)

考虑到你要做的事情(这是计算一个组密钥,在这种情况下是事务的类型),下面的代码为你做了。

public class Receipt
{
    public ATender Tender { get; set; } 
}

public class ATender
{
    public string TenderType { get; set;}
    public string TenderName { get; set;}
}

void Main()
{   
    IEnumerable<Receipt> ReceiptPayments= new [] { 
        new Receipt { Tender=new ATender {TenderName="1tender", TenderType="1"}},
        new Receipt { Tender=new ATender {TenderName="2tender", TenderType="2"}},
        new Receipt { Tender=new ATender {TenderName="3tender", TenderType="1"}}
    };

    int result = ReceiptPayments.GroupBy(x=>x.Tender).Where(g=>g.Key.TenderType == "1").Count();
}