Quantcast
Channel: User learning... - Stack Overflow
Viewing all articles
Browse latest Browse all 38

LINQ distinct on groups of information

$
0
0

I have multiple items (List)

I need to get distinct features for the items but the issue is that each item has two features. So both features need to match on GroupTypeId and GroupId to become a distinct group.

I need to group items that have the same features (per above distinct groups found). I don't need features here again at the item level since I will have these per above in a separate object.

I need to keep items order intact, the first item will go in group 1, then second might go in group 1 or group 2 and so on.

Also, each item in group, item number needs to be overwritten per the new sequence in that group.

Can i do above tasks purely with LINQ rather than using nested loops?

In the below sample for items

  • i have 3 distinct feature groups

  • and 3 item groups. Item 2 and 4 needs to be grouped together and the line no needs to change to 1 and 2. For item 1 and 3, line number should become as 1.

Need to add to List whose count will be 3

  • Index[0] will have 2 features and 1 item
  • Index[1] will have 2 features and 2 items
  • Index[2] will have 2 features and 1 item
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 numberforeach(var pg in pickedGroupings){    var lineNum = 1;    foreach(var item in pg.Items)    {        item.LineNumber = lineNum;        lineNum += 1;    }}

Viewing all articles
Browse latest Browse all 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>