1
我有這個查詢從一堆商店中檢索銷售,並將它們作爲列表返回。在我的報告請求屏幕上,用戶可以通過多個ID進行過濾 - ShopOwnerId,ShopRegionId,ShopTypeCode等。正確過濾Linq查詢
我的查詢的結構是在選定日期之間獲得所有銷售額,然後根據選擇進行過濾。這顯然非常低效。 :
private List<Sales> GetFilteredListOfSales(Request reportreq)
{
ModelContainer ctn = new ModelContainer();
List<ShopSale> shopsSales = new List<shopsale>();
// If no filters are selected
//
if (reportreq.RegionalId == null && reportreq.OwnerId == null && reportreq.ShopTypeCode == null)
{
shopsSales = (from sale in ctn.ShopSales
where sale.DateSold >= reportreq.FromDate && sale.DateSold <= reportreq.ToDate
select sale).ToList();
}
// If the regional ID has a value...
//
if (reportreq.RegionalId.HasValue)
{
shopsSales = (from sale in ctn.ShopSales
where sale.Shop.Owner.RegionalId == reportreq.RegionalId
&& sale.DateSold >= reportreq.FromDate && sale.DateSold <= reportreq.ToDate
select sale).ToList();
}
// If the owner ID has a value...
//
if (reportreq.OwnerId.HasValue)
{
shopsSales = (from sale in ctn.ShopSales
where sale.Shop.OwnerId == reportreq.OwnerId
&& sale.DateSold >= reportreq.FromDate && sale.DateSold <= reportreq.ToDate
select sale).ToList();
}
if (!string.IsNullOrEmpty(reportreq.ShopTypeCode))
{
shopsSales = (from sale in ctn.ShopSales
where sale.Shop.ShopTypeCode.ToUpper().Contains(reportreq.ShopTypeCode.ToUpper())
&& sale.DateSold >= reportreq.FromDate && sale.DateSold <= reportreq.ToDate
select sale).ToList();
}
return shopsSales;
}
您可以看到,此方法僅在未選擇過濾器的情況下查詢特定日期之間的銷售表。
我堅持的是,用戶可以選擇多個過濾器 - 例如,他們可能會選擇一個regionId和一個ShopTypeCode,所以我不想爲每個if過濾器查詢ctn.ShopSales,因爲如果選擇了多個選項,它將消除以前檢索的值。
有沒有人有任何建議如何解決這個問題?如果需要更多信息,請問!
感謝
嗨,喬恩,這似乎是完美的。簡單但高效。謝謝,一旦時限到了,就會標記爲正確。 – 109221793