小编Tom*_*asz的帖子

使用LINQ的条件组By语句

我有一个看似相当简单的要求,但环顾四周我无法得到一个简单的答案.我查看过MSDN论坛,Exper Exchange并没有给我任何实质内容.

我有以下LINQ代码

Dim SummaryLog As IQueryable(Of clPartCountSummary)    
SummaryLog = From Inventory In db.tblPartCounts _
            Where Inventory.InventoryCountId = InventoryCountId _
            And Not Inventory.ActionId.HasValue _
            Group By PartNumber = Inventory.PartNumber _
            , Inventory.RevLevel, SAPLocation = Inventory.SAPLocation _
            Into AggregatedProdLog = Group, Qty = Sum(Inventory.Quantity) _
            Select New clPartCountSummary With 
                {.PartNumber = PartNumber, 
                 .RevLevel = RevLevel, 
                 .Qty = Qty, 
                 .SAPLocation = SAPLocation}
Run Code Online (Sandbox Code Playgroud)

我希望能够有条件地分组RevLevelSAPLocation.我将永远分组PartNumber,但其他两个是可选的.因此,如果变量bRevLevel为真,那么我们分组RevLevel,如果bSAPLocation为真,那么我们也分组SAPLocation.

任何帮助将不胜感激,我正处于多个SummaryLog定义开始看起来很吸引人的阶段.

谢谢,托马斯

c# linq conditional if-statement group-by

4
推荐指数
1
解决办法
5747
查看次数

标签 统计

c# ×1

conditional ×1

group-by ×1

if-statement ×1

linq ×1