将foreach替换为queryable循环



我有一个函数返回一个报告对象,但目前我正在通过foreach查看,然后使用asQueryable方法。

我想在一个查询中完成它,而不必使用AsQueryable函数。

var query = from item in context.Dealers
            where item.ManufacturerId == manufacturerId
            select item;
IList<DealerReport> list = new List<DealerReport>();
foreach (var deal in query)
{
  foreach (var bodyshop in deal.Bodyshops1.Where(x => x.Manufacturer2Bodyshop.Select(s => s.ManufacturerId).Contains(manufacturerId)))
  {
      DealerReport report = new DealerReport();
      report.Dealer = deal.Name;
      report.Bodyshop = bodyshop.Name;
      short stat = bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x => x.ManufacturerId == manufacturerId).ComplianceStatus;
      report.StatusShort = stat;
      list.Add(report);
   }
}
return list.OrderBy(x => x.Dealer).AsQueryable();

我想你需要这样的东西:

var query = from deal in context.Dealers
            where deal.ManufacturerId == manufacturerId
            from bodyshop in deal.Bodyshops1
            where bodyshop.Manufacturer2Bodyshop.Select(s => s.ManufacturerId).Contains(manufacturerId)
            let stat = bodyshop.Manufacturer2Bodyshop.FirstOrDefault(x => x.ManufacturerId == manufacturerId)
            orderby deal.Name
            select new DealerReport
            {
                Dealer = deal.Name,
                Bodyshop = bodyshop.Name,
                StatusShort = stat != null ? stat.ComplianceStatus : 0, // or some other default
            };
return query;

相关内容

  • 没有找到相关文章