2011-10-12 70 views
0

我真的很困惑我需要的报告。截至今天,我的大部分报告都很简单,所以我能够轻松地做到这一点。但作为在SQL/DLINQ一个新手,我无法通过以下找到我的方式:如何将5种不同类型的linq查询合并到一个列中

var closingStock = 
    (from p in session.Query<Product>() 
    select new 
    { 
     p.Id, 
     p.Name, 
     p.Batch, 
     p.Rate, 
     ClosingStock = p.Quantity - p.AllocatedQuantity, 
     p.DivisionId 
    }).ToList(); 

var distributedQuantityAfterPeriod = 
    (from i in session.Query<OutwardInvoiceItem>() 
    where i.ParentInvoice.Date > ToDate 
    select new 
    { 
     Id = i.Product.Id, 
     DistributedAfter = i.Quantity 
    }).ToList(); 

var distributedQuantityInPeriod = 
    (from i in session.Query<OutwardInvoiceItem>() 
    where i.ParentInvoice.Date >= FromDate && i.ParentInvoice.Date <= ToDate 
    select new 
    { 
     Id = i.Product.Id, 
     Distributed = i.Quantity 
    }).ToList(); 

var receivedQuantityAfterPeriod = 
    (from i in session.Query<InwardInvoiceItem>() 
    where i.ParentInvoice.Date > ToDate 
    select new 
    { 
     Id = i.Product.Id, 
     ReceivedAfter = i.Quantity 
    }).ToList(); 

var receivedQuantityInPeriod = 
    (from i in session.Query<InwardInvoiceItem>() 
    where i.ParentInvoice.Date >= FromDate && i.ParentInvoice.Date <= ToDate 
    select new 
    { 
     Id = i.Product.Id, 
     Received = i.Quantity 
    }).ToList(); 

正如你所看到的,我想建立一个特定日期的库存移动报告。我遇到以下问题:
1.如何减少五个查询?可能吗?
2.我该如何将这些查询提供的数据合并到一个表中,该表按产品ID进行分组并汇总为数量相关的列?到目前为止,我正在使用非常慢的循环。

我用的是什么:
C#4,NHibernate的,SQLite的

任何帮助将非常高度赞赏。

Regards, Yogesh。

回答

1
  1. 减少往返使用.Future()代替.List()

  2. 让所有的查询返回

    group i by i.Id into g 
    select new 
    { 
        Id = g.Key, 
        Quantity = g.Sum(x => x.Quantity) 
    }).Future(); 
    

,做

var alltogether = groupedDistributedQuantityAfterPeriod 
    .Concat(groupedDistributedQuantityInPeriod) 
    .Concate(...); 

from g in alltogether 
group g by g.key into all 
select new 
{ 
    Id = all.Key, 
    Quantity = all.Sum(x => x.Quantity) 
}; 

UPD吃:

可以减少查询的数量与

from i in session.Query<OutwardInvoiceItem>() 
where (i.ParentInvoice.Date > ToDate) || (i.ParentInvoice.Date >= FromDate && i.ParentInvoice.Date <= ToDate) 
select ... 

from i in session.Query<InwardInvoiceItem>() 
where (i.ParentInvoice.Date > ToDate) || (i.ParentInvoice.Date >= FromDate && i.ParentInvoice.Date <= ToDate) 
select ... 
+0

非常感谢。我已经想出了使用linq合并所有表的方法。但是使用Future和Concat确实给了我更快的结果。如果没有关于减少查询数量的答案,我会选择你的答案。 – Yogesh

相关问题