【问题标题】:How to get list of results in Linq如何在 Linq 中获取结果列表
【发布时间】:2021-10-12 12:27:04
【问题描述】:

我正在获取一个计数列表,根据条件平均并返回所有结果。看来我的命令太错误了。我还不知道该怎么做。 我有桌子:

var qr = (from Product c in _context.Products
                      join o in _context.RatingProducts on c.ID equals o.IDProduct
                      where c.Status == true
                      orderby c.NumProduct
                      group o by c.ID into g
                      select new {
                          ProductId = g.Key,
                          CountID = g.Count(),
                          //SumID = g.Sum(s => s.StarRating).ToString(),
                          AveragedID = g.Count() > 0 ? g.Average(x => x.StarRating) : 0
                      }).ToList();

        var getqr = _context.Products.Where(a => a.Status).ToList();

        int count = 0;
        double averaged = 0;

        foreach (var item in getqr)
        {
            int key = item.ID;
            var id = qr.Where(x => x.ProductId == key).FirstOrDefault();
            if(id != null)
            {
                count = id.CountID;
                averaged = Math.Round(id.AveragedID,1);
            }
            var productquery = item;
        }

        var query = from c in qr
                    join p in getqr on c.ProductId equals p.ID
                    select new ProductQuery
                    {
                        ID = p.ID,
                        IDProduct = p.IDProduct,
                        CategoryID = p.CategoryID,
                        NumProduct = p.NumProduct,
                        Name = p.Name,
                        Status = p.Status,
                        Price = p.Price,
                        PriceOld = p.PriceOld,
                        ContentsSmall = p.ContentsSmall,
                        Contents = p.Contents,
                        Images = p.Images,
                        CountID = count,
                        //SumID = c.SumID,
                        AveragedID = averaged,
                    };

        return query.ToList();

但是,当我调试时,它只显示一个行 ID = 1 的结果。我想显示列表:计数,表 1 的平均结果。Tks。

【问题讨论】:

  • 请尝试更清楚地解释您要达到的目标。
  • 是的,我已经更清楚地更新了解释。

标签: linq entity-framework-core


【解决方案1】:

据我了解,您应该在这里设置LEFT JOIN,因此需要在初始查询中添加DefaultIfEmpty,并稍微更改平均计数逻辑。

这样的东西,应该可以工作:

var qr = (from Product c in _context.Products
                  join o in _context.RatingProducts on c.ID equals o.IDProduct into productRatings
                  from pr in productRatings.DefaultIfEmpty()
                  where c.Status == true
                  orderby c.NumProduct
                  group o by c.ID into g
                  select new {
                      ProductId = g.Key,
                      CountID = g.Count(),
                      AveragedID = g.Average(x => x == null ? 0 : x.StarRating)
                  }).ToList();

【讨论】:

  • 感谢您的回答。但是,我收到一条错误消息:“当前上下文中不存在名称 'o'”。 Position: "group o by c.ID into g".
  • 应该是from o in productRatings.DefaultIfEmpty()
  • 我试过了,但是 row ID = 3 count CountID = 1????。我想要 CountID = 0
  • 我已经解决了这个问题。谢谢
猜你喜欢
  • 2011-12-08
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多