1

我正在尝试在 LINQ 上执行此操作:

select p.ProductID, p.ArticleCode, SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])
from Products p
inner join StockItems s on p.ArticleCode = s.ArticleCode
inner join StockHistorical h on s.ArticleID = h.ArticleID
where h.[Date] < '23/08/2013 11:30:00'
group by p.ProductID, p.ArticleCode

我有这个:

        var products = (from p in e.Products
                        join s in e.StockItems on p.ArticleCode equals s.ArticleCode
                        join h in e.StockHistoricals on s.ArticleID equals h.ArticleID
                        where h.Date < DateTime.Parse("23/08/2013 11:30:00")
                        group p by p.ProductID into StockResult
                        select new {  });

任何人都知道我该怎么做

SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])

与 LINQ?

谢谢!

我忘了说问题是“分组依据”,因为我无法访问 StockResult 组中的 OperationType 属性。

解决了!关键是:

let combined = new 
{
    Product = p,
    HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}
4

3 回答 3

2
...
let combined = new 
{
    Product = p,
    HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}
group combined by combined.Product.ProductID into StockResult
select new
{
    ProductID = StockResult.Key,
    Total = StockResult.Sum(x => x.HistoryCount)
}
于 2013-09-19T14:00:31.010 回答
0

这不是直接翻译,但我会选择:

(h.Count(his => his.OperationType != 0) 
    - h.Count(his => his.OperationType == 0))
    * h.Count()

它应该在功能上等效,并且似乎更好地代表了您正在尝试做的事情。

于 2013-09-19T14:00:38.953 回答
0

你正在尝试做这样的事情吗?我不确定这是否可行。

select new
{
S=Sum(((h.OperationType)!=0?-1:1)*h.Count)
}
于 2013-09-19T14:03:44.100 回答