using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System; namespace CompareUtility { struct Item { int id; int size; } public class Compare { List<Item> lstA = new List<Item>(); List<Item> lstB = new List<Item>(); public void Init() { //好像下面不能这样增加元素 lstA.Add(new Item() { 1, 10 }); lstA.Add(new Item() { 2,10}); lstA.Add(new Item() { 3, 10 }); lstA.Add(new Item() { 4, 10 }); lstB.Add(new Item() { 1, 10 }); lstB.Add(new Item() { 2, 10 }); lstB.Add(new Item() { 3, 10 }); lstB.Add(new Item() { 5, 10 }); } //比较新增 即B有 A无 public List<Item> CompareAdd() { //var lst = lstB.Except(lstA); //return lst; //求方法 } //比较相同 即A和B都有的 public List<Item> CompareSome() { //求 方法 } } }
解决方案
40
namespace CompareUtility { class Program { static void Main(string[] args) { Compare c = new Compare(); List<Item> list1 = c.CompareAdd(); foreach (Item item in list1) { Console.WriteLine("{0},{1}", item.id, item.size); } List<Item> list2 = c.CompareSome(); foreach (Item item in list2) { Console.WriteLine("{0},{1}", item.id, item.size); } } } public struct Item { public int id; public int size; } public class Compare { List<Item> lstA = new List<Item>(); List<Item> lstB = new List<Item>(); public Compare() { lstA.Add(new Item() { id = 1, size = 10 }); lstA.Add(new Item() { id = 2, size = 10 }); lstA.Add(new Item() { id = 3, size = 10 }); lstA.Add(new Item() { id = 4, size = 10 }); lstB.Add(new Item() { id = 1, size = 10 }); lstB.Add(new Item() { id = 2, size = 10 }); lstB.Add(new Item() { id = 3, size = 10 }); lstB.Add(new Item() { id = 5, size = 10 }); } /// <summary> /// 比较新增 即B有 A无 /// </summary> public List<Item> CompareAdd() { return lstB.Except(lstA).ToList<Item>(); } /// <summary> /// 比较相同 即A和B都有的 /// </summary> public List<Item> CompareSome() { return lstB.Except(lstB.Except(lstA)).ToList<Item>(); } } }