Wednesday, 15 January 2014

c# - Group items by content of list -


Let's say I have something like this:

  public class subscriber {public string name {get; Set;}} public class SomeData {public string content {get; Set;}} public class inputdata {public subscriber member {receives; Set; } Public IEnumerable & lt; SomeData & gt; Get Datasites { Set; }} Public Class QiTem {public INIMERABLE & lt; Subscriber & gt; Members {receive; Set; } Public IEnumerable & lt; SomeData & gt; Get Datasites { Set; }}  

Now I say that I can get a from the list & lt; InputData & gt;

QueueItem> I now compare the list of data of each customer and with a list & Lt want to end 1 , if 2 customers have the same set of data items, then, they will be QueueItem .

Hopefully it makes sense

using technology enumerable . Equality Comparison with Equity ()

  Public class members {public string name {get; Set; } // Similar to comparing public override balls (object obj) {return string. Ecloss (this.Name, (subscriber) obj) .name); } Public override int GetHashCode () {return.Name.GetHashCode (); }} Public class SomeData {public string content {get; Set; } // Comparison of public override balls (object oBJE) {return string Eckels (this. Resources, (some data) oz). } Public override intoshosh () {return it back. Content.gatehashcode (); }} Public Sector InputData {PUBLIC SUBSCRIBER MEMBER {Get; Set; } Public IEnumerable & lt; SomeData & gt; Get Datasites { Set; } // should always begin to emulate a blank collection public () {this.DataItems = new list & lt; SomeData & gt; (); }} Public Class QiTem {public INIMERABLE & lt; Subscriber & gt; Members {receive; Set; } Public IEnumerable & lt; SomeData & gt; Get Datasites { Set; } // should always open a blank collection public QueueItem (this Subscribers = new list & subscribe & gt; (); This.DataItems = New list & lt; SomeData & gt; ();}} Public Class Datatoins Aquality Comparor: Equity Comparator & lt; IEnumerable & lt; SomeData & gt; & gt; {Public Override Bool Equals (IEnumerable & LT; SomeData & gt; X, IEnumerable & lt; SomeData & gt; y) {Return Enumerable .equenceEqual (x.OrderBy (i => i.Content), y.OrderBy (i = & gt; i.Content));} Public override integer GetHashCode (IEnumerable & lt; SomeData & gt; obj) {obj .Select Return (i = & gt; i.GetHashCode ()) Yoga () GetHashCode () ..}}  

Use

  var data = new list & lt; InputData & gt; (); var fruit = new [] (new) some data () {content = "apple"}, something new Data () {content = "pear"}}} Var color = new [] (new) some data () {content = "red"}, some new data () {content = "blue"}, new data () {Content = "green"}}; Add data (new input data) {Subscribers = New subscriber () {name = "Alice"}, DataTime = New list & lt; Something & gt; (Fruit)}); Data Add (new inputdata) {member = new member () {name = "bob"}, datatm = new list & lt; SomeData & gt; (Colour)}); Data Add (New Input Data) {Subscribers = New Subscriber () {Name = "Charlie"}, DataTime = New List & lt; Something & gt; (Fruit)}); & Lt; QueueItem & gt; Grouped data = group Group (i = & gt; i.DataItems, i = & gt; i.Subscribers, New DataItemsEqualityComparer ()). Select (i => New QueueItem () {Subscribers = i, DataItems = i.Key}). To create the list ();  

Result

  QueueItem: Member: - Alice - Charlie Data: - Apple - Pear QiEtium: Member: - Bob Data: - Red - Blue - Green  

No comments:

Post a Comment