日期:2014-05-17 浏览次数:20471 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class wage { public int id { set; get; } public string card { set; get; } public string name { set; get; } public decimal price { set; get; } } class MyComparer : IEqualityComparer<wage> { public bool Equals(wage x, wage y) { return x.card == y.card && x.name == y.name && x.price == y.price; } public int GetHashCode(wage obj) { return obj.card.GetHashCode() ^ obj.name.GetHashCode() ^ obj.price.GetHashCode(); } } class Program { static void Main(string[] args) { List<wage> list1 = new List<wage>() { new wage() { id = 1, card = "card1", name = "name1", price = 1 }, new wage() { id = 2, card = "card2", name = "name2", price = 2 }, new wage() { id = 3, card = "card3", name = "name3", price = 3 }, new wage() { id = 5, card = "card5", name = "name5", price = 5 }, new wage() { id = 6, card = "card8", name = "name8", price = 8 } }; List<wage> list2 = new List<wage>() { new wage() { id = 1, card = "card1", name = "name1", price = 2 }, new wage() { id = 2, card = "card2", name = "name2", price = 2 }, new wage() { id = 3, card = "card3", name = "name4", price = 3 }, new wage() { id = 4, card = "card4", name = "name4", price = 4 }, new wage() { id = 5, card = "card8", name = "name8", price = 8 } }; var result = list1.Union(list2, new MyComparer()).Except(list1.Intersect(list2, new MyComparer()), new MyComparer()); foreach (var item in result) { Console.WriteLine("id = {0}, card = {1}, name = {2}, price = {3}", item.id, item.card, item.name, item.price); } } } }