C# 使用LINQ的条件GROUPBY语句

C# 使用LINQ的条件GROUPBY语句,c#,linq,if-statement,group-by,conditional,C#,Linq,If Statement,Group By,Conditional,我有一个似乎相当简单的要求,但环顾四周,我无法得到一个简单的答案。我看过MSDN论坛,Exper Exchange,但没有给我任何实质性的东西 我有以下LINQ代码 Dim SummaryLog As IQueryable(Of clPartCountSummary) SummaryLog = From Inventory In db.tblPartCounts _ Where Inventory.InventoryCountId = InventoryCount

我有一个似乎相当简单的要求,但环顾四周,我无法得到一个简单的答案。我看过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}
我希望能够在
RevLevel
SAPLocation
上有条件地分组。我将始终按
零件号
分组,但其他两个是可选的。因此,如果变量
bRevLevel
为真,则我们按
RevLevel
分组,如果
bsapolocation
为真,则我们也按
SAPLocation
分组

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

谢谢,托马斯兹希望这对你有帮助

public class TblPartCount
{
    public int InventoryCountID { get; set; }
    public int? ActionID { get; set; }
    public string PartNumber { get; set; }
    public int RevLevel { get; set; }
    public string SAPLocation { get; set; }
    public int Quantity { get; set; }
}

private static void ConditionalGroupBy()
    {
        bool pCheck = true;

        var lList = new List<TblPartCount>
                        {
                            new TblPartCount { InventoryCountID = 1, ActionID = 2, PartNumber = "123", RevLevel = 1, SAPLocation = "AAAA", Quantity = 100 },
                            new TblPartCount { InventoryCountID = 1, ActionID = 2, PartNumber = "123", RevLevel = 1, SAPLocation = "BBBB", Quantity = 200 }
                        };

        var lOutput = lList
                          .Where(pArg => pArg.InventoryCountID == 1)
                          .Where(pArg => pArg.ActionID != null)
                          .GroupBy(pArg => new
                          {
                              PartNumber = pArg.PartNumber,
                              RevLevel = pCheck ? pArg.RevLevel : 0,
                              SAPLocation = pCheck ? pArg.SAPLocation : String.Empty
                          })
                          .Select(pArg =>
                                    new
                                    {
                                        PartNumber = pArg.Key.PartNumber,
                                        RevLevel = pArg.Key.RevLevel,
                                        SAPLocation = pArg.Key.SAPLocation,
                                        Qtry = pArg.Sum(pArg1 => pArg1.Quantity)
                                    });

        foreach (var lItem in lOutput)
        {
            Console.WriteLine(lItem);
        }
    }
公共类TBLPARCount
{
public int InventoryCountID{get;set;}
public int?ActionID{get;set;}
公共字符串零件号{get;set;}
public int RevLevel{get;set;}
公共字符串位置{get;set;}
公共整数数量{get;set;}
}
私有静态void ConditionalGroupBy()
{
bool pCheck=true;
var lList=新列表
{
新的TblPartCount{InventoryCountID=1,ActionID=2,PartNumber=“123”,RevLevel=1,SAPLocation=“AAAA”,Quantity=100},
新的TblPartCount{InventoryCountID=1,ActionID=2,PartNumber=“123”,RevLevel=1,SAPLocation=“BBBB”,Quantity=200}
};
var lOutput=lList
.Where(pArg=>pArg.InventoryCountID==1)
.Where(pArg=>pArg.ActionID!=null)
.GroupBy(pArg=>new
{
零件号=零件号,
RevLevel=pCheck?参数RevLevel:0,
SAPLocation=pCheck?pArg.SAPLocation:String.Empty
})
.选择(参数=>
新的
{
零件号=pArg.Key.PartNumber,
RevLevel=pArg.Key.RevLevel,
SAPLocation=pArg.Key.SAPLocation,
数量=总数量(总数量=>总数量)
});
foreach(百叶窗中的var lItem)
{
控制台写入线(lItem);
}
}