Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Compare list
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { var l1 = new List<ls1>(); for(int i=0; i<10; i++) { var l = new ls1(); l.id = i; l.Lista = "L1"; l1.Add(l); } var l2 = new List<ls1>(); var l22 = new ls1(); l22.id = 1; l22.Lista = "L1"; l2.Add(l22); var l11 = new ls1(); l11.id = 3; l11.Lista = "L1"; l2.Add(l11); Console.WriteLine("Lista 1"); foreach(var item in l1) { Console.Write(item.id + " " + item.Lista + ", "); } Console.WriteLine(); Console.WriteLine("Lista 2"); foreach(var item in l2) { Console.Write(item.id + " " + item.Lista + ", "); } Console.WriteLine(); var result = l1.Except(l2, new CompararLista()).ToList(); string ret = null; Console.WriteLine("Itens da lista 1 not in 2"); foreach(var item in result) { Console.Write(item.id + " " + item.Lista + ", "); } Console.WriteLine(ret); var result2 = l1.Intersect(l2, new CompararLista()).ToList(); string ret2 = null; Console.WriteLine("Itens da lista 1 in 2"); foreach(var item in result2) { Console.Write(item.id + "-" + item.Lista + ", "); } Console.WriteLine(ret2); IEnumerable<ls1> except = l1.Except(l2, new CompararLista()); foreach (var product in except) Console.Write(product.id + " " + product.Lista + ", "); } public class ls1 { public int id {get;set;} public string Lista {get;set;} } class CompararLista : IEqualityComparer<ls1> { // Products are equal if their names and product numbers are equal. public bool Equals(ls1 x, ls1 y) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(x, y)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; //Check whether the products' properties are equal. return x.id == y.id && x.Lista == y.Lista; } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public int GetHashCode(ls1 product) { //Check whether the object is null if (Object.ReferenceEquals(product, null)) return 0; //Get hash code for the Name field if it is not null. int hashProductName = product.Lista == null ? 0 : product.Lista.GetHashCode(); //Get hash code for the Code field. int hashProductCode = product.id.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; } } } }
run
|
edit
|
history
|
help
0
Extension Method Sample
3
іфи
sss0
Calculadora
project euler 18
ASCII -> Braille ASCII
Shifting Elements Forward in a List, and Random Numbers Generation
Jump Search
Truncate String by Byte