简体   繁体   中英

Linq to SQL to return multiple counts from query not returning correct results

I have a Linq to SQL query that I want to return three counts for number of rows. One for where one field where it does not equal zero (HaveCount), another for another field when it does not equal zero (WantCount), and another for another field when it does not equal zero (SaleCount).

Here is my query...

var counts = (from a in dc.tblMemberIssues
              join b in dc.vwMembers on a.MemberID equals b.MemberID
              where a.IssueID == issueID
              group a by new { a.HaveCount, a.WantCount, a.SaleCount } into d
              select new
              {
                  HaveCount = d.Count(e => e.HaveCount != 0),
                  WantCount = d.Count(e => e.WantCount != 0),
                  SaleCount = d.Count(e => e.SaleCount != 0)
              }).First();

However it does not return the expected results. I realise the grouping is wrong, but I'm not sure how to get the desired results.

For example if first part of the query (before the grouping) returned these two rows...

---------------------------------
HaveCount | WantCount | SaleCount
---------------------------------
1         | 0         | 1
1         | 1         | 0

My query now is returning... (ie. just one row)

1         | 0         | 1

But I want it to return counts from all rows...

2         | 1         | 1

What do I need to do to my query to make it work they way I need?

NB. Trying to do this with only a single database query.

You should not group on the columns in the count. If I understand you correctly you don't want to group on anything, but Linq does not allow that so put true as the group by. (or any other constant) Change your query to:

var counts = (from a in dc.tblMemberIssues
              join b in dc.vwMembers on a.MemberID equals b.MemberID
              where a.IssueID == issueID
              group a by true into d
              select new
              {
                  HaveCount = d.Count(e => e.HaveCount != 0),
                  WantCount = d.Count(e => e.WantCount != 0),
                  SaleCount = d.Count(e => e.SaleCount != 0)
              }).First();

It looks like you need to save your counts to a list, then take the count of each type of count, like so:

var counts = (from a in dc.tblMemberIssues
    join b in dc.vwMembers on a.MemberID equals b.MemberID
    where a.IssueID == issueID).ToList();

var results = new
{
    HaveCount = counts.Count(e => e.HaveCount != 0),
    WantCount = counts.Count(e => e.WantCount != 0),
    SaleCount = counts.Count(e => e.SaleCount != 0)
};

It's also possible you would want the sum instead:

var results = new
{
    HaveCount = counts.Sum(e => e.HaveCount),
    WantCount = counts.Sum(e => e.WantCount),
    SaleCount = counts.Sum(e => e.SaleCount)
};

If you just want to count the number of non-zeroes even if some of the initial counts can be greater than 1, use the first query, otherwise, use the second query.

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