2017-08-01 58 views
0

我有一個奇怪的要求。我根據某些標準對記錄進行分組(例如,Gln和其他字段......)。對於每個組,我都有進一步的對象列表。我意識到來自這些列表的幾個對象必須是分開的,並且需要使用相同的Gln複製另一個組。因爲這些物體不應該放置在那裏。他們必須制定另一組具有相同密鑰的組織(即Gln)。如何從父母和子組中使用Linq提取子組

請看下面的代碼。

var claimsQuery = (from consignment in Consignments 
       let consigneeAddress = consignment.ConsignmentAddresses.FirstOrDefault(pr => pr.AddressTypeId == 2) 
       where (consignment.ClientId == 26 && consignment.FixDate != null) 
       group consignment by new 
       {            
        consigneeAddress.GLN, 
        consignment.ForwarderId, 
        consigneeAddress.Country.ISO, 
        consignment.FixDate, 
        ProductName = Products.FirstOrDefault(pr => pr.ProductId == consignment.ProductId).Name, 
        ProductId = consignment.ProductId 
       } 
       into g 
       select new DepoOverviewData 
       { 

        ForwarderId = g.Key.ForwarderId, 
        FixDate = g.Key.FixDate.GetValueOrDefault(), 
        Gln = g.Key.GLN, 
        ProductName = g.Key.ProductName,             

        ConsignmentsOverviewData = 
          (from singleConsignment in Consignments.Where(pr => (pr.FixDate == g.Key.FixDate) && (pr.ProductId == g.Key.ProductId))    
          join x in ConsignmentAddresses on singleConsignment.ConsignmentId equals x.ConsignmentId 
          where x.GLN == g.Key.GLN && x.AddressTypeId == 2 

          select new ConsignmentClaimsOverviewData() 
          {           
            ConsignmentId = singleConsignment.ConsignmentId, 
            Weight = singleConsignment.Weight, 
            ChargeableWeight = singleConsignment.ChargeableWeight, 
            TotalCost = singleConsignment.NetTotalCost, 
            ProductId = singleConsignment.ProductId, 
            ProductName = Products.FirstOrDefault(pr => pr.ProductId == singleConsignment.ProductId).Name, 
            FixDate = singleConsignment.FixDate, 
            ExpressTime = singleConsignment.ExpressTime, 
            ClientOrderNumber = singleConsignment.ClientOrderNumber, 
            DeliveryDate = singleConsignment.DeliveryDate, 
            InTime = false, // this will be reset later. (i.e True, false or null after iterating again over each group). 

          }).ToList() 
       }).ToList(); 

我還附上了一張圖片來演示。 enter image description here

+2

如果您創建一個較小的輸入示例和所需的輸出 –

回答

0

由於您未能指定何時將組拆分爲兩個(或多個)的條件,請讓我給您一個通用示例。

它背後的想法是將另一個屬性添加到指示屬於「組中的組」的組中。我使用bool將組拆分爲兩個,但當然這可以更改爲具有不止兩個值的類型。

如果輸入的是像

var input = new[] { 
    new { Key = 1, Value = 42 }, 
    new { Key = 1, Value = 101 }, 
    new { Key = 1, Value = 99 }, 
    new { Key = 2, Value = 999 }, 
    new { Key = 2, Value = 1001 }, 
    new { Key = 3, Value = 4711 } 
}; 

以及是否要創建一個單獨的組規則是

private static bool NeedsSeparateGroup(int key, int value) 
{ 
    switch (key) 
    { 
     case 1: return value > 100; 
     case 2: return value > 1000; 
     default: return false; 
    } 
} 

然後就組也由什麼規定屬於該組的組(唐「T喜歡查詢語法,當然你可以用它):

var result = input 
    .GroupBy(raw => new 
    { 
     raw.Key, 
     IsSeparateGroup = NeedsSeparateGroup(raw.Key, raw.Value) 
    }, (k, vs) => new 
    { 
     k.Key, 
     k.IsSeparateGroup, 
     Values = vs.Select(r => r.Value).ToList() 
    }); 

Key: 1, IsSeparateGroup: False, Values: 42,99 
Key: 1, IsSeparateGroup: True, Values: 101 
Key: 2, IsSeparateGroup: False, Values: 999 
Key: 2, IsSeparateGroup: True, Values: 1001 
Key: 3, IsSeparateGroup: False, Values: 4711 
+0

謝謝tinudu,將會更容易幫助您。它就像你說的那樣工作。我非常感謝你的幫助 – Usman