LINQ在信息组上有所不同

时间:2019-06-15 09:25:45

标签: c# linq

我有多个项目(列表)

我需要为这些商品提供鲜明的功能,但问题是每个商品都有两个功能。因此,这两个功能都需要在GroupTypeId和GroupId上进行匹配才能成为不同的组。

我需要对具有相同功能的项目进行分组(根据上面找到的不同分组)。我不需要在项目级别再次使用这些功能,因为我会在一个单独的对象中将这些功能放在上面。

我需要保持项目顺序不变,第一个项目进入组1,然后第二个项目进入组1或组2,依此类推。

此外,组中的每个项目,项目编号都需要根据该组中的新序列进行覆盖。

我可以仅使用LINQ而不是使用嵌套循环来完成上述任务吗?

在下面的示例项目

  • 我有3个不同的特征组

  • 和3个项目组。需要将项目2和4分组在一起,并且无需将行更改为1和2。对于项目1和3,行号应变为1。

需要添加到计数为3的列表中

  • 索引[0]将具有2个功能和1个项目
  • 索引[1]将具有2个功能和2个项目
  • 索引[2]将具有2个功能和1个项目
public class ItemPicked
{
    public int Id { get; set; }
    public string Description { get; set; }
    public int LineNumber { get; set; }
    public int PartId { get; set; }
    public List<ItemFeature> Features { get; set; }
}
public class ItemFeature
{
    public string OriginalReceived { get; set; }
    public Group Feature { get; set; }
}

public class Group
{
    public int Id { get; set; }
    public string GroupTypeId { get; set; }
    public string GroupId { get; set; }
    public int SequenceNo { get; set; }
}

public class PickedGrouping
{
    public List<ItemFeature> Features { get; set; }
    public List<ItemPicked> Items { get; set; }
}


var SampleFeatures1 = new List<ItemFeature>() {
    new ItemFeature {
        OriginalReceived = "SomeThing",
        Feature = new Group() {
            Id = 1, SequenceNo = 1, GroupTypeId = "A", GroupId = "B"
        }
    },
    new ItemFeature {
        OriginalReceived = "SomeThing2",
        Feature = new Group() {
            Id = 2, SequenceNo = 2, GroupTypeId = "Y", GroupId = "Z"
        }
    }
};

var SampleFeatures2 = new List<ItemFeature>() {
    new ItemFeature {
        OriginalReceived = "SomeThing3",
        Feature = new Group() {
            Id = 3, SequenceNo = 3, GroupTypeId = "C", GroupId = "D"
        }
    },
    new ItemFeature {
        OriginalReceived = "SomeThing4",
        Feature = new Group() {
            Id = 4, SequenceNo = 4, GroupTypeId = "X", GroupId = "Y"
        }
    }
};

var SampleFeatures3 = new List<ItemFeature>() {
    new ItemFeature {
        OriginalReceived = "SomeThing5",
        Feature = new Group() {
            Id = 3, SequenceNo = 3, GroupTypeId = "C", GroupId = "D"
        }
    },
    new ItemFeature {
        OriginalReceived = "SomeThing5",
        Feature = new Group() {
            Id = 2, SequenceNo = 2, GroupTypeId = "M", GroupId = "K"
        }
    }
};

var items = new List<ItemPicked>(){
    new ItemPicked{
        Id = 1, Description = "Item 1", LineNumber = 1, Features = SampleFeatures1
    },
    new ItemPicked{
        Id = 2, Description = "Item 2", LineNumber = 2, Features = SampleFeatures2
    },
    new ItemPicked{
        Id = 3, Description = "Item 3", LineNumber = 3, Features = SampleFeatures3
    },
    new ItemPicked{
        Id = 4, Description = "Item 4", LineNumber = 4, Features = SampleFeatures2
    }
};


var pickedGroupings = new List<PickedGrouping>();
PickedGrouping selectedGroup = null;

foreach (var item in items)
{
    var found = 0;
    if(item.Features == null || !item.Features.Any())
    {
        selectedGroup = pickedGroupings.FirstOrDefault(x => x.Features == null || !x.Features.Any());
        if (selectedGroup == null) selectedGroup = new PickedGrouping();
        selectedGroup.Features.AddRange(item.Features);
    }
    else
    {
        foreach (var feature in item.Features)
        {
            foreach (var pg in pickedGroupings)
            {
                if ((item.Features == null || !item.Features.Any()) && (pg.Features == null || !pg.Features.Any())){
                    selectedGroup = pg;
                    found += 1;
                }
                else
                {
                    foreach (var pgf in pg.Features)
                    {
                        if (pgf.Feature == null) continue;
                        if (pgf.Feature.GroupId == feature.Feature.GroupId && pgf.Feature.GroupTypeId == feature.Feature.GroupTypeId)
                        {
                            selectedGroup = pg;
                            found += 1;
                        }
                    }
                }
            }
        }
        if (found < 2)
        {
            pickedGroupings.Add(new PickedGrouping() { Features = item.Features });
            selectedGroup = pickedGroupings[pickedGroupings.Count - 1];
        }
    }

    //add item
    if (selectedGroup.Items == null) selectedGroup.Items = new List<ItemPicked>();
    selectedGroup.Items.Add(item);
}

//update line number
foreach(var pg in pickedGroupings)
{
    var lineNum = 1;
    foreach(var item in pg.Items)
    {
        item.LineNumber = lineNum;
        lineNum += 1;
    }
}

0 个答案:

没有答案