简体   繁体   中英

Group items by content of list

Lets say i got a something like this:

public class Subscriber{
    public string Name {get; set;}  
}

public class SomeData{
    public string Content {get; set;}   
}

public class InputData {
    public Subscriber Subscribers { get; set; }
    public IEnumerable<SomeData> DataItems { get; set; }
}

public class QueueItem {
    public IEnumerable<Subscriber> Subscribers { get; set; }
    public IEnumerable<SomeData> DataItems { get; set; }
}

Now lets say i get a List<InputData> full of "Subscribers" with a list of data for each subscriber.

Now i want to compare the list of data of each subscriber, and end up with a List<QueueItem> , where if 2 subscribers have the same set of data items, they would be 1 QueueItem .

Hope this makes sense

The technique is using EqualityComparer with Enumerable.SequenceEqual()

public class Subscriber
{
    public string Name { get; set; }

    // For compare
    public override bool Equals(object obj) { return string.Equals(this.Name, ((Subscriber)obj).Name); }
    public override int GetHashCode() { return this.Name.GetHashCode(); } 
}

public class SomeData
{
    public string Content { get; set; }

    // For compare
    public override bool Equals(object obj) { return string.Equals(this.Content, ((SomeData)obj).Content); }
    public override int GetHashCode() { return this.Content.GetHashCode(); } 
}

public class InputData
{
    public Subscriber Subscribers { get; set; }
    public IEnumerable<SomeData> DataItems { get; set; }

    // Should always initialize an empty collection
    public InputData() { this.DataItems = new List<SomeData>(); }
}

public class QueueItem
{
    public IEnumerable<Subscriber> Subscribers { get; set; }
    public IEnumerable<SomeData> DataItems { get; set; }

    // Should always initialize an empty collection
    public QueueItem() { this.Subscribers = new List<Subscriber>(); this.DataItems = new List<SomeData>(); }
}

public class DataItemsEqualityComparer : EqualityComparer<IEnumerable<SomeData>>
{

    public override bool Equals(IEnumerable<SomeData> x, IEnumerable<SomeData> y)
    {
        return Enumerable.SequenceEqual(x.OrderBy(i => i.Content), y.OrderBy(i => i.Content));
    }

    public override int GetHashCode(IEnumerable<SomeData> obj)
    {
        return obj.Select(i => i.GetHashCode()).Sum().GetHashCode();
    }
}

Usage

var data = new List<InputData>();
var fruits = new[] { new SomeData() { Content = "apple" }, new SomeData() { Content = "pear"} };
var colors = new[] { new SomeData() { Content = "red" }, new SomeData() { Content = "blue" }, new SomeData() { Content = "green" } };

data.Add(new InputData() { Subscribers = new Subscriber() { Name = "Alice" }, DataItems = new List<SomeData>(fruits) });
data.Add(new InputData() { Subscribers = new Subscriber() { Name = "Bob" }, DataItems = new List<SomeData>(colors) });
data.Add(new InputData() { Subscribers = new Subscriber() { Name = "Charlie" }, DataItems = new List<SomeData>(fruits) });

List<QueueItem> groupedData = data.GroupBy(
    i => i.DataItems, 
    i => i.Subscribers, 
    new DataItemsEqualityComparer())
    .Select(i => new QueueItem() { Subscribers = i, DataItems = i.Key }).ToList();

Result

QueueItem :
  Subscribers:
  - Alice
  - Charlie
  Data:
  - apple
  - pear
QueueItem :
  Subscribers:
  - Bob
  Data:
  - red
  - blue
  - green

 var queue = Dictionary(Subscriber, List<SomeData>); //And lets just for example add some data var items1 = new List<SomeData>(); items1.Add(new SomeData("test")); items1.Add(new SomeData("test2")); var items2 = new List<SomeData>(); items2.Add(new SomeData("test")); queue.Add(new Subscriber("Peter"), items1); queue.Add(new Subscriber("Luke"), items1); queue.Add(new Subscriber("Anna"), items2); Dictionary<Subscriber, List<SomeData>> myDictionary = queue .GroupBy(o => o.PropertyName) .ToDictionary(g => g.Key, g => g.ToList()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM