.NET List常見操作


一、簡單類型List的交集並集差集

1、先定義兩個簡單類型的List

List<int> listA = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
List<int> listB = new List<int>() { 1, 2, 3, 4, 9 };

 

2、取兩個List的並集

var resultUnionList= listA.Union(listB).ToList();

執行結果如下:

  

3、取兩個List的交集

var resultIntersectList = listA.Intersect(listB);

執行結果如下:

  

4、取兩個List的差集,差集是指取在該集合中而不在另一集合中的所有的項

var resultExceptList = listA.Except(listB);

執行結果如下:

  

二、對象List集合的交集並集差集

1、先定義一個類

    /// <summary>
    /// 學生類
    /// </summary>
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Sex { get; set; }
    }

 

2、定義兩個List

            //LISTA
            List<Student> stuListA = new List<Student>();
            stuListA.Add(new Student
            {
                Name = "A1",
                Age = 10,
                Sex = ""
            });
            stuListA.Add(new Student
            {
                Name = "A2",
                Age = 11,
                Sex = ""
            });

            //LISTB
            List<Student> stuListB = new List<Student>();
            stuListB.Add(new Student
            {
                Name = "B1",
                Age = 10,
                Sex = ""
            });
            stuListB.Add(new Student
            {
                Name = "B2",
                Age = 11,
                Sex = ""
            });            

 

3、取上述兩個list集合的並集

var result = stuListA.Union(stuListB).ToList();

 

4、取上述兩個list集合的交集,應為是對象集合,可以根據一定規則 Func<TSource, bool> predicate限定那些屬於交集

(1)取兩個對象集合中對象名稱一樣的交集

var result = stuListA.Where(x => stuListB.Any(e => e.Name == x.Name)).ToList();

  

(2)取兩個對象集合中對象名稱、對象年齡、對象性別都一樣的交集

var result = stuListA.Where(x => stuListB.Any(e => e.Name == x.Name && e.Age == x.Age && e.Sex == x.Sex)).ToList();

 

5、取上述兩個list集合的差集,可以根據一定規則 Func<TSource, bool> predicate限定那些屬於差集

(1)取差集,根據兩個對象集合中對象名稱一樣的規則取差集

var result = stuListA.Where(x =>! stuListB.Any(e => e.Name == x.Name)).ToList();

  

(2)取差集,根據兩個對象集合中對象名稱、對象年齡、對象性別都一樣的規則取差集

var result = stuListA.Where(x => !stuListB.Any(e => e.Name == x.Name && e.Age == x.Age && e.Sex == x.Sex)).ToList();

三、List<string>和List<int>互相轉換

List<string> 轉 List<int>

var list = (new[]{"1","2","3"}).ToList();
var newlist = list.Select<string,int>(x =>Convert.ToInt32(x));

List<int> 轉List<string> 

List<int> list = new List<int>(new int[] { 1,2,3 } );
List<string> newList = list.ConvertAll<string>(x => x.ToString());

四、List排重

1、使用linq提供的Distinct方法

public class Test
{ 
     public int ID { get; set; }
     public string Name { get; set; }
}
復制代碼
public class TestMain
{
  public static void TestMothod()
  {
     List<Test> testList = new List<Test>();
      testList.Add(new Test { ID = 1, Name = "小名" });
      testList.Add(new Test { ID = 1, Name = "小紅" });
      testList.Add(new Test { ID = 2, Name = "小名" });
      //通過使用默認的相等比較器對值進行比較返回序列中的非重復元素。
      List<Test> tempList = testList.Distinct<Test>().ToList();
   }
}
復制代碼

2、根據某個字段排除重復項

添加一個擴展排重擴展方法:

復制代碼
 public static class DistinctExtension
 {
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
 }
復制代碼

使用上述擴展方法:

復制代碼
public class TestMain
{
  public static void TestMothod()
   {
       List<Test> testList = new List<Test>();
       testList.Add(new Test { ID = 1, Name = "小名" });
       testList.Add(new Test { ID = 1, Name = "小紅" });
       testList.Add(new Test { ID = 2, Name = "小名" });
       //根據某個字段排除重復項。
       List<Test> tempList = testList.DistinctBy(p => p.ID).ToList();
   }
}
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM