首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对组和使用LINQ

对组和使用LINQ
EN

Stack Overflow用户
提问于 2015-12-28 21:24:11
回答 1查看 112关注 0票数 2

我很难从linq查询中获得分组和。我正试图在一定的日期范围内得到产品的净变化。现在,我的查询运行,并返回每个事务的总数量变化,但我无法得到和。我的查询如下:

代码语言:javascript
复制
from t in Transactions
join p in Products on t.Product.ID equals p.ID
where t.TransactionDate >= DateTime.Parse("12/01/2015") && t.TransactionDate <= DateTime.Parse("12/31/2015")
orderby p.ProductCode
select new 
{
    Product = p.ProductCode,
    Description = p.Description,
    TotalQuantityChanged = (t.TransactionType.AddRemove == "Addition" ? (t.FullQuantity + (t.PartialQuantity / p.Pieces)).ToString() : (-1 * (t.FullQuantity + (t.PartialQuantity / p.Pieces))).ToString() )
}

这就回来了

代码语言:javascript
复制
Product     Description                         TotalQuantityChanged

B107        3 ½" x 11" x 105" Kraft Faced R-11          23 
B107        3 ½" x 11" x 105" Kraft Faced R-11          -16 
X13AK       3 ½" x 11" x 105" Kraft Faced R-13          65 
X13AK       3 ½" x 11" x 105" Kraft Faced R-13          45 
X13AK       3 ½" x 11" x 105" Kraft Faced R-13          -12 
X45EX       3 ½" x 15" x 105" Kraft Faced R-15 HD       3 
X45EX       3 ½" x 15" x 105" Kraft Faced R-15 HD       36 
X45EX       3 ½" x 15" x 105" Kraft Faced R-15 HD       -7 

但我已经试过各种方式的团体和总和,没有运气。结果应该是这样的:

代码语言:javascript
复制
Product     Description                         TotalQuantityChanged

B107        3 ½" x 11" x 105" Kraft Faced R-11          7 
X13AK       3 ½" x 11" x 105" Kraft Faced R-13          98 
X45EX       3 ½" x 15" x 105" Kraft Faced R-15 HD       32

我尝试过这样分组,但是在where子句和select语句中出现了一个错误。

代码语言:javascript
复制
from t in Transactions
join p in Products on t.Product.ID equals p.ID 
group p by new { 
        p.ProductCode, 
        p.Description 
    } into g
where t.TransactionDate >= DateTime.Parse("12/01/2015") && t.TransactionDate <= DateTime.Parse("12/31/2015")
select new
{
    Product = g.Key.ProductCode,
    Description = g.Key.Description,
    TotalQuantityChanged = (t.TransactionType.AddRemove == "Addition" ? (t.FullQuantity + (t.PartialQuantity / p.Pieces)).ToString() : (-1 * (t.FullQuantity + (t.PartialQuantity / p.Pieces))).ToString() )
}

我不知道如何分组或得到这笔钱。如能提供任何协助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-28 21:32:26

这大概就是你想要的:

代码语言:javascript
复制
from t in Transactions
join p in Products on t.Product.ID equals p.ID
where t.TransactionDate >= DateTime.Parse("12/01/2015") && t.TransactionDate <= DateTime.Parse("12/31/2015")
group t.TransactionType.AddRemove == "Addition" 
           ? t.FullQuantity + (t.PartialQuantity / p.Pieces) 
           : -1 * (t.FullQuantity + (t.PartialQuantity / p.Pieces)) 
    by new {p.ProductCode, p.Description} into g
select new 
{
    Product = g.Key.ProductCode,
    Description = g.Key.Description,
    TotalQuantityChanged = g.Sum().ToString()
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34500978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档