使用linq将集合列表分组到集合列表中

时间:2016-08-18 15:04:36

标签: c# linq

我需要根据对某些属性的分组创建新列表,并使用相同的方法生成MVC视图。

我有以下结构

class ItemLine
{
   string SomeProperty { get; set; } 
   string SomeProperty1 { get; set; } 
   string SomeProperty2 { get; set; } 
}

class Item
{
   IEnumerable<ItemLine> ItemLines { get; set; }
}

Class Order
{
   IEnumerable<Item> Items { get; set; }
}

项目类作为模型接收到视图。 现在我想基于someProperty

创建多个IEnumerable

e.g。

-Order-
       -Item-
              -ItemLine-
                         -someProperty = 123-
                         -someProperty1-
                         -someProperty2-
              -ItemLine-
                         -someProperty = 234-
                         -someProperty1-
                         -someProperty2-
              -ItemLine-
                         -someProperty = 345-
                         -someProperty1-
                         -someProperty2-
       -Item-
              -ItemLine-
                         -someProperty = 123-
                         -someProperty1-
                         -someProperty2-
              -ItemLine-
                         -someProperty = 456-
                         -someProperty1-
                         -someProperty2-
       -Item-
              -ItemLine-
                         -someProperty = 345-
                         -someProperty1-
                         -someProperty2-


Output needed is


    -Order-
           -Item-
                  -ItemLine-
                             -someProperty = 123-
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-
                             -someProperty = 123-
                             -someProperty1-
                             -someProperty2-    
                  -ItemLine-
                             -someProperty = null/blank-
                             -someProperty1-
                             -someProperty2-                                 
            -Item-               
                  -ItemLine-
                             -someProperty = 234-
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-             
                             -someProperty = null/blank-
                             -someProperty1-
                             -someProperty2-    
                  -ItemLine-             
                             -someProperty = null/blank-
                             -someProperty1-
                             -someProperty2-                                 
           -Item-

                  -ItemLine-
                             -someProperty = 345-
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-
                             -someProperty = 345-
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-             
                             -someProperty = null/blank-
                             -someProperty1-
                             -someProperty2-                                 
            -Item-               
                  -ItemLine-
                             -someProperty = 456-
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-
                             -someProperty = null/blank
                             -someProperty1-
                             -someProperty2-
                  -ItemLine-
                             -someProperty = null/blank
                             -someProperty1-
                             -someProperty2-

我尝试了很多东西,但是我无法实现这一点,请使用linq建议如何做到这一点。

感谢!!!

更新:我已尝试过关注,但它会输出重复内容,我认为这不是最佳做法。

其中一个要求是新的列表/集合必须具有与集合中任何Item中的ItemLines的最大数量相同的ItemLines数量,其中“extra”ItemLines具有其属性的null,空值或默认值。

foreach (var items in Model)
{
    foreach (var item in hm.Items)
    {
        var x = Model.SelectMany(u => u.Items).Where(u => u.ItemNo.Equals(item.ItemNo)).ToList();
    }
}

1 个答案:

答案 0 :(得分:0)

这可能不是最好的方法,但是因为你有条件需要让所有def reviewOrder(request, id): order = get_object_or_404(models.Order, id=id) if order.type == 'retail': total = models.Price.calculate_price(order.dry_wood, order.wet_wood, order.briquette, order.stelinka) context = {'order': order} context.update(total) return render(request, 'reviewOrder.html', context=context) return render(request, 'reviewOrder.html', context={'order': order}) def delete_order(request, id): order = get_object_or_404(models.Order, id=id) order.delete() return HttpResponseRedirect('/') def confirm_order(request, id): order = get_object_or_404(models.Order, id=id) order.confirmed = True order.save() if not order.backuped: models.OrderBackup.objects.create(original_id=order.id,**model_to_dict(order, exclude=['id','customer','backuped' ])) order.backuped = True notifications.AdminNotifications.new_order(order) notifications.CustomerNotifications.orderHasBeenCreated(order) return HttpResponseRedirect('/') 包含相同数量的Item s,这就是我能想到的out(并希望看到别人会怎么做):

ItemLine

<强>更新

鉴于所做的评论,我假设您的// Group the data out of your model organized by the SomeProperty value // Order it by the number of items in the list so we know the first group will have the maximum number of values we'd need var orderedGroups = Model.Items .SelectMany(itm => itm.ItemLines) .GroupBy(itm => itm.SomeProperty) .OrderByDescending(grp => grp.Count()); // Find out how many ItemLines you'd want to have in any 1 Item var count = orderedGroups.First().Count(); // Create the output var output = new Order() { Items = orderedGroups .Select(grp => new Item() { ItemLines = new ItemLine[count] .Select((s,i) => grp.ElementAtOrDefault(i) ?? new ItemLine()) } )}; 只是Model的IEnumerable,而不是Item。既然如此,只需将第一行替换为:

Order

其余的都可以保持不变。

希望这可以解决问题。