如何在LINQ中重用部分选择代码编写聚合查询



我有一个可重用的选择查询方法,由另外两个方法调用。

这些是我在我的项目中的样例程。SalesQry()方法在我们的项目中实际上非常复杂。

Private IEnumerable<Sales> SalesQry(SalesDataContext sContext, int customerID)
{
   return 
      from SALE in sContext.Sales
      where SALE.CustomerID = customerID
      select SALE
}
Public List<SalesAGG> SalesByArea()
{
   SalesDataContext oSContext = new SalesDataContext (.....);
  return from SALE in SalesQry(sContext,1230)
              group SALE by
              new
              {
                Type = SALE.SaleType,
                Area = SALE.Area
              }   into aggAREA
             select new  SalesAGG()
             {
                Type = aggAREA.Key.Type,
                Place =  aggAREA.Key.Area,
                TotalSales = aggAREA.Count()
             }.ToList();     
}
Public List<SalesAGG> SalesByState()
{
   SalesDataContext oSContext = new SalesDataContext (.....);
  return from SALE in SalesQry(sContext,1230)
              group SALE by
              new
              {
                Type = SALE.SaleType,
                State = SALE.State
              }   into aggSTATE
             select new  SalesAGG()
             {
                Type = aggSTATE.Key.Type,
                Place =  aggSTATE.Key.State,
                TotalSales = aggSTATE.Count()
             }.ToList();            
}

我遇到的问题是,当SalesByState()或SalesByArea()函数执行sql server不运行聚合查询,而是只是运行此部分返回大量行

from SALE in sContext.Sales
          where SALE.CustomerID = customerID
          select SALE

,其余部分在应用程序内部执行。如果我不调用函数SalesQry()然后像下面这样修改查询,SQL server运行一个聚合查询,返回的行数非常少。

 from SALE in sContext.Sales
 where SALE.CustomerID = 1230
 group SALE by
  new
  {
     Type = SALE.SaleType,
     State = SALE.State
   }   into aggSTATE
  select new  SalesAGG()
  {
    Type = aggSTATE.Key.Type,
    Place =  aggSTATE.Key.State,
   TotalSales = aggSTATE.Count()
  }.ToList();

我需要分享代码,因为它非常复杂,并且在许多其他地方使用。如何编写查询,使sql server在服务器上运行整个聚合查询,并使用函数SalesQry()

Edit:这可能是您返回IEnumerable<Sales>而不是IQueryable<Sales>。我第一次没听明白。

我相信它是被迫在客户端上执行聚合,因为它不能在数据库服务器上运行new SalesAGG()。试着用Let语句把它取出来,例如:

return from SALE in SalesQry(sContext,1230)
          group SALE by
          new
          {
            Type = SALE.SaleType,
            Area = SALE.Area
          }   into aggAREA
         let totalSales = aggAREA.Count()
         select new  SalesAGG()
         {
            Type = aggAREA.Key.Type,
            Place =  aggAREA.Key.Area,
            TotalSales = totalSales
         }.ToList();   

如果这不起作用,那么我的下一个预感是group by子句对新创建的对象进行分组。尝试使用字符串连接作为group by标准,例如:

return from SALE in SalesQry(sContext,1230)
          group SALE by SALE.SaleType + SALE.Area into aggAREA
         select new  SalesAGG()
         {
            Type = aggAREA.First().Type,
            Place =  aggAREA.First().Area,
            TotalSales = aggAREA.Count()
         }.ToList();   

相关内容

  • 没有找到相关文章