简体   繁体   中英

Issue with LINQ group by with count

I'm trying to run the following query but for some reason MemberTransactionCount and NonMemberTransactionCount are coming back as the exact same values. It seems that the .Where() clauses aren't working as we'd expect them to.

Hoping someone can point out where I might be going wrong.

from trans in transactions
orderby trans.TransactionDate.Year , trans.TransactionDate.Month
group trans by new {trans.TransactionDate.Year, trans.TransactionDate.Month}
into grp
select new MemberTransactions
{
    Month = string.Format("{0}/{1}", grp.Key.Month, grp.Key.Year),
    MemberTransactionCount =
        grp.Where(x => x.Account.Id != Guid.Empty || x.CardNumber != null)
           .Sum(x => x.AmountSpent),
    NonMemberTransactionCount =
        grp.Where(x => x.Account.Id == Guid.Empty && x.CardNumber == null)
           .Sum(x => x.AmountSpent)
}

EDIT I've verified in the database that the results are not what they should be. It seems to be adding everything together and not taking into account the Account criteria that we're looking at.

I ended up solving this with two separate queries. It's not exactly as I wanted, but it does the job and seems to just as quick as I would have hoped.

var memberTrans = from trans in transactions
              where trans.Account != null
                    || trans.CardNumber != null
              orderby trans.TransactionDate.Month
              group trans by trans.TransactionDate.Month
              into grp
              select new
                  {
                      Month = grp.Key,
                      Amount = grp.Sum(x => x.AmountSpent)
                  };
var nonMemberTrans = (from trans in transactions
                  where trans.Account == null
                        && trans.CardNumber == null
                  group trans by trans.TransactionDate.Month
                  into grp
                  select new
                      {
                          Month = grp.Key,
                          Amount = grp.Sum(x => x.AmountSpent)
                      }).ToList();

var memberTransactions = new List<MemberTransactions>();
foreach (var trans in memberTrans)
{
var non = (from nt in nonMemberTrans
           where nt.Month == trans.Month
           select nt).FirstOrDefault();

var date = new DateTime(2012, trans.Month, 1);
memberTransactions.Add(new MemberTransactions
    {
        Month = date.ToString("MMM"),
        MemberTransactionCount = trans.Amount,
        NonMemberTransactionCount = non != null ? non.Amount : 0.00m
    });
}

I think the main problem here is that you doubt the result, though it might be correct.

Add another property for verification:

TotalAmount = grp.Sum(x => x.AmountSpent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM