【问题标题】:Conditional Group By statement using LINQ使用 LINQ 的条件分组依据语句
【发布时间】:2010-11-01 05:52:43
【问题描述】:

我有一个看似相当简单的要求,但环顾四周,我无法得到一个简单的答案。我看过 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}

我希望能够按RevLevelSAPLocation 有条件地分组。我将始终按PartNumber 分组,但其他两个是可选的。因此,如果变量bRevLevel 为真,则我们按RevLevel 分组,如果bSAPLocation 为真,则我们也按SAPLocation 分组。

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

谢谢,托马斯

【问题讨论】:

    标签: c# linq if-statement group-by conditional


    【解决方案1】:

    希望这会有所帮助....

    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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      相关资源
      最近更新 更多