如何在 LINQ 的“选择”部分中有条件地使用 Sum 方法

本文关键字:有条件 方法 Sum LINQ 选择 | 更新日期: 2023-09-27 18:31:46

我有以下查询:

var query = from comment in SH_Comments
        join user in Users
        on comment.UserID equals user.UserID
        join product in SH_Products
        on comment.ProductID equals product.ID
        join vote in SH_CommentVotes
        on comment.ID equals vote.CommentID
        group comment by new
        {
            comment.Body,
            comment.CreatedDate,
            comment.IsApproved,
            comment.IsRead,
            comment.ID,
            comment.ProductID,
            comment.UserID,
            comment.ParentID,
            user.DisplayName,
            user.Username,
            product.Title,
            vote.IsPositive
        } into g
        select new
        {
            g.Key.Body,
            g.Key.CreatedDate,
            g.Key.IsApproved,
            g.Key.IsRead,
            g.Key.ID,
            g.Key.ProductID,
            g.Key.UserID,
            g.Key.ParentID,
            g.Key.DisplayName,
            g.Key.Username,
            g.Key.Title,
            PositiveVotes = g.Where(c=>g.Key.IsPositive).Sum(c=>g.Key.ID),
            NegativeVotes = g.Where(c=>!g.Key.IsPositive).Sum(c=>g.Key.ID)
        };

我想通过表中的 IsPositive 字段获取PositiveVotesNegativeVotes计数。如果我的方式是错误的,你能告诉我真正的方法吗?

我收到以下错误: Column 't4.Body' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

我该如何解决这个问题?

谢谢

如何在 LINQ 的“选择”部分中有条件地使用 Sum 方法

我得到了真正的方法,所以我为你们把我的方式放在这里!

var query = from comment in SH_Comments
        join user in Users
        on comment.UserID equals user.UserID
        join product in SH_Products
        on comment.ProductID equals product.ID
        join vote in SH_CommentVotes
        on comment.ID equals vote.CommentID
        group comment by new
        {
            comment.Body,
            comment.CreatedDate,
            comment.IsApproved,
            comment.IsRead,
            comment.ID,
            comment.ProductID,
            comment.UserID,
            comment.ParentID,
            user.DisplayName,
            user.Username,
            product.Title,
            vote.IsPositive
        } into g
        select new
        {
            g.Key.Body,
            g.Key.CreatedDate,
            g.Key.IsApproved,
            g.Key.IsRead,
            g.Key.ID,
            g.Key.ProductID,
            g.Key.UserID,
            g.Key.ParentID,
            g.Key.DisplayName,
            g.Key.Username,
            g.Key.Title,
            PositiveVotes = g.Where(c=>g.Key.IsPositive).Count(),
            NegativeVotes = g.Where(c=>!g.Key.IsPositive).Count()
        };      
var list = query
        .GroupBy(x => new
        { 
            x.Title,
            x.Body,
            x.CreatedDate,
            x.IsApproved,
            x.IsRead,
            x.ID,
            x.ProductID,
            x.UserID,
            x.ParentID,
            x.DisplayName,
            x.Username,
        })
        .Select(x => new 
        { 
            x.Key.Body,
            x.Key.CreatedDate,
            x.Key.IsApproved,
            x.Key.IsRead,
            x.Key.ID,
            x.Key.ProductID,
            x.Key.UserID,
            x.Key.ParentID,
            x.Key.DisplayName,
            x.Key.Username,
            x.Key.Title,
            PositiveVotes = x.Sum(s => s.PositiveVotes),
            NegativeVotes = x.Sum(s => s.NegativeVotes) 
        });

感谢Gábor Bakos的精彩评论。

干杯!