0

我有一个返回报表对象的函数,但目前我正在查看 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();
4

1 回答 1

2

我想你想要这样的东西:

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;
于 2015-06-08T12:01:29.133 回答