0
var commodity = _appDbContext.ArchivesCCommodity.Where(lambda)
          .GroupJoin(_appDbContext.ArchivesCCommoditySpecification, a => a.Code, b => b.Commodity, (a, b) => new { a, b })
          .SelectMany(a => a.b.DefaultIfEmpty(), (a, b) => new { a.a, b })
          .GroupJoin(_appDbContext.ArchivesCSpecificationDetail, a => a.a.a.b.SpecificationDetail, d => d.Code, (a, d) => new { a, d })
          .SelectMany(a => a.d.DefaultIfEmpty(), (a, d) => new
          {
              Commodity = a.a.a.Code,
              CommodityName = a.a.a.Name,
              SpecificationDetailName = d.Name,
              OrderSN = d.OrderSN
          }).AsQueryable().OrderBy(a => a.OrderSN).GroupBy(a => new { a.Commodity, a.CommodityName })
           .Select(a => new
           {
               Commodity = a.Key.Commodity,
               CommodityName = a.Key.CommodityName,
               SpecificationDetailName = string.Join(" - ", a.Select(a => a.SpecificationDetailName)),
               SpecificationDetailTotal = string.Join(" - ", a.Select(a => a.SpecificationDetailName)) == "" ? 0 : a.Count()
           }); 

其中 .AsQueryable() 会导致错误

.AsQueryable()
.OrderBy(a => a.OrderSN)
.GroupBy(a => new { a.Commodity, a.CommodityName })

在此处输入图像描述

改成AsEnumerable()不会报错

.ASEnumerable()
.OrderBy(a => a.OrderSN)
.GroupBy(a => new { a.Commodity, a.CommodityName })

但是我暂时不想把这段代码发送到数据库中,因为它会在分页查询之后发送。我不知道怎么处理?

///////////////我粘贴了完整的代码,并讨论了我的实际需求

查询代码,逐页查询数据库。例如,只检查一页和 10 行记录。这里没问题。

            var AA= _appDbContext.ArchivesCCommodity.Where(lambda)
              .GroupJoin(_appDbContext.ArchivesCCommoditySpecification, a => a.Code, b => b.Commodity, (a, b) => new { a, b })
              .SelectMany(a => a.b.DefaultIfEmpty(), (a, b) => new { a.a, b })
              .GroupJoin(_appDbContext.ArchivesCSpecificationDetail, a => a.a.b.SpecificationDetail, d => d.Code, (a, d) => new { a, d })
              .SelectMany(a => a.d.DefaultIfEmpty(), (a, d) => new
              {
                  Commodity = a.a.a.a.a.Code,
                  CommodityName = a.a.a.a.a.Name,
                  SpecificationDetailName = d.Name,
                  OrderSN = d.OrderSN
              });

            PageHealper<object> page = new PageHealper<object>();

            page.Start(pageNum, pageSize);

            page = await page.RestPage(AA);

这时候我又分组整理了一下,现在发现:

  1. 不是操作分页查询结果,而是查询所有AA数据库。
  2. 根据前面的分页查询,得到行数和页码。在这里,通过分组和合并来改变行数。这就是为什么我想把分组和排序放在一起,最后是分页。
  var BB = AA.AsEnumerable().OrderBy(a => a.OrderSN).GroupBy(a => new { a.Commodity, a.CommodityName, a.Specification, a.SpecificationName })
               .Select(a => new
               {
                   Commodity = a.Key.Commodity,
                   CommodityName = a.Key.CommodityName,
                   SpecificationDetailName = string.Join(" - ", a.Select(a => a.SpecificationDetailName)),
                   SpecificationDetailTotal = string.Join(" - ", a.Select(a => a.SpecificationDetailName)) == "" ? 0 : a.Count()
               }); ;

            page.Data = BB.ToList<object>();
            return page;
4

1 回答 1

0

查看这篇文章https://weblogs.asp.net/zeeshanhirani/using-asqueryable-with-linq-to-objects-and-linq-to-sql关于 AsQueryable 的作用。

我认为您在那里并不真的需要 AsQueryable ... LINQ to SQL 不喜欢该查询的某些内容。

它不喜欢 String.Join(...) 因为它不能翻译它。

因此,您可以做的一件事是将 .AsEnumerable() 放在 GroupBy() 之后,这将在 SQL 中完成所有操作,在内存中完成所有操作。

前任:

var commodity = _appDbContext.ArchivesCCommodity.Where(lambda)
        .GroupJoin(_appDbContext.ArchivesCCommoditySpecification, a => a.Code, b => b.Commodity, (a, b) => new { a, b })
        .SelectMany(a => a.b.DefaultIfEmpty(), (a, b) => new { a.a, b })
        .GroupJoin(_appDbContext.ArchivesCSpecificationDetail, a => a.a.a.b.SpecificationDetail, d => d.Code, (a, d) => new { a, d })
        .SelectMany(a => a.d.DefaultIfEmpty(), (a, d) => new
        {
            Commodity = a.a.a.Code,
            CommodityName = a.a.a.Name,
            SpecificationDetailName = d.Name,
            OrderSN = d.OrderSN
        }).OrderBy(a => a.OrderSN).GroupBy(a => new { a.Commodity, a.CommodityName })
        .AnEnumerable()
        .Select(a => new
        {
            Commodity = a.Key.Commodity,
            CommodityName = a.Key.CommodityName,
            SpecificationDetailName = string.Join(" - ", a.Select(a => a.SpecificationDetailName)),
            SpecificationDetailTotal = string.Join(" - ", a.Select(a => a.SpecificationDetailName)) == "" ? 0 : a.Count()
        }); 
于 2021-07-20T01:37:27.797 回答