Muthu Annamalai
Muthu Annamalai

Reputation: 91

Linq Group Count Items

I have this linq to entity

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
  }

This is working fine, except the count property, it is just giving the number of group count. I don't know how to find out each Tender Count

Upvotes: 1

Views: 459

Answers (1)

krystan honour
krystan honour

Reputation: 6793

Given what you are trying to do (which is to count a groups keys, which in this case is the type of transaction) the following code does that for you.

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();
}

Upvotes: 1

Related Questions