linq 如何加入两个IQueryable列表?

7lrncoxx  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(150)

我想通过左连接group by each的结果值来获得值。

var mgga = mggb.GroupBy(i => i.GoodsCode).Select(i => i.MaxBy(x => x.PriceChangeDate));
        var oega = oegb.GroupBy(i => i.GoodsCode).Select(i => i.MinBy(x => x.EventSalePrice));

像这样加入,

var result = from mgg in mgga
                        join oeg in oega
                        on new { storeCode = mgg.StoreCode, goodsCode = mgg.GoodsCode } equals new { storeCode = oeg.StoreCode, goodsCode = oeg.GoodsCode } into mgoe
                        from oeg in mgoe.DefaultIfEmpty()
                        select new {
                            GoodsCode = mgg.GoodsCode,
                        };

但是,这显示错误消息。

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: The LINQ expression 'g
          .AsQueryable()
          .Select(e => new {
              ...
           })
          .AsQueryable()
          .MaxBy(x => x.PriceChangeDate)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

我如何加入类型为IQueryable〈'a〉的mgga或oga?

jqjz2hbq

jqjz2hbq1#

尝试以下查询实现。其他部分应该可以工作。

var mgga = mggb.GroupBy(i => i.GoodsCode)
    .Select(g => g.OrderByDescending(x => x.PriceChangeDate).First());
var oega = oegb.GroupBy(i => i.GoodsCode)
    .Select(g => g.OrderByDescending(x => x.EventSalePrice).First());

相关问题