简体   繁体   English

Linq to SQL从查询返回多个计数而不返回正确的结果

[英]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. 我有一个Linq to SQL查询,我想返回三个行数计数。 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). 一个用于不等于零的字段(HaveCount),另一个用于不等于零的字段(WantCount),以及另一个不等于零的字段(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. 如果我理解正确的话,你不希望任何东西组,但LINQ的不允许这样就把true为组通过。 (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: 看来您需要将counts保存到列表中,然后获取每种计数类型的计数,如下所示:

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. 如果即使某些初始计数可能大于1,如果您只想计算非零的数量,请使用第一个查询,否则,请使用第二个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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