c#中List的操作 元素遍歷(foreach)和去重復(distinct)


c#中List的元素遍歷(foreach)和去重復(distinct)

 

var lst_rpeat = lst_all.GroupBy(x =>
new { x.a, x.b,x.c, x.d,
x.e,x.f }).Where(x => x.Count() > 1).ToList();

lst_commission_rpeat.ForEach(x =>
{
string _strError ="Lines:(";
var plist = x.ToList();
plist.ForEach(p=> {
_strError += (lst_all.IndexOf(p)+1).ToString() + ",";
});
_strError += ") Repeat;";
errorMsg.AppendLine(_strError);
});

一、准備工作

定義實體類people

public List PeopleList { get; set; }

public class People { public string Name { get; set; } public int Age { get; set; } }

實體比較help類 public delegate bool CompareDelegate(T x, T y);

public class ListCompare : IEqualityComparer { private CompareDelegate _compare;

public ListCompare(CompareDelegate d) { this._compare = d; }

public bool Equals(T x, T y) { if (_compare != null) { return this._compare(x, y); } else { return false; } }

public int GetHashCode(T obj) { return obj.ToString().GetHashCode(); } }

二、List.ForEach() 假設需要對集合中的每個元素進行運算(將每個人的年齡增加10歲)

PeopleList.ForEach(p=>{ p.Age = p.Age + 10; });

三、List.Distinct() 假設需要將姓名和年齡相同的元素過濾掉

PeopleList.Distinct(new Common.List.ListCompare( (x,y)=> x.Name==y.Name&&x.Age==y.Age) );

解析: ListCompare類用來比較List中的兩個元素。它的構造函數中需要傳入一個CompareDelegate。

可以看出,兩個元素的比較,重點在CompareDelegate中。

定義: public delegate bool CompareDelegate(T x, T y);

其實,ListCompare實現了IEqualityComparer接口。

List data = myDalJD.GetAllDataList();

List list= new List(); list= datalist4.Select(p => p.name).ToList(); //只取name字段,重新生成新的List集合 this.cmbJdUserName.DataSource = list.Distinct().ToList(); //去重復,綁定數據后面要加ToList()   

int tem2 = list.FindLastIndex(x => x == "Jack");//通過lambda-expressions // Display the first structure

List lists=new List();

A a1=new A();

a.str="a"; a.bol=false; a.dec=1; lists.Add(a1);

var querylist=lists.Where(x=>x.bol==false).ToList();  

 

 

List集合查詢數據

List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });
All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)確定序列中的所有元素是否都滿足條件。
復制代碼
Any<TSource>(IEnumerable<TSource>) 確定序列是否包含任何元素。
//表示集合中的任一個元素滿足表達式條件,即返回true。
var res1 = employees.Any(e => e.Id == 3 || e.Id == 8);
Console.WriteLine(res1);  //out:True

var res2 = employees.Any(e => e.Id == 3 && e.Name == "Steven");
Console.WriteLine(res2);  //out:False  數據源Name中沒有Steven
復制代碼
Contains(T) 確定某元素是否在 List<T> 中。
Equals(Object) 確定指定的對象是否等於當前對象。(繼承自 Object)
Exists(Predicate<T>) 確定 List<T> 是否包含與指定謂詞定義的條件匹配的元素。
復制代碼
Find(Predicate<T>) 搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中的第一個匹配元素。
復制代碼
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });

//如果找到與指定謂詞定義的條件匹配的第一個元素,則為該元素;
var emp = employees.Find(e => e.Id == 5);
Console.WriteLine(emp.Id);          //5
Console.WriteLine(emp.Name);        //Steven.Buchanan
Console.WriteLine(emp.City);        //London
Console.WriteLine(emp.BirthDate);   //1955-03-04
復制代碼
復制代碼
復制代碼
FindAll(Predicate<T>) 檢索與指定謂詞定義的條件匹配的所有元素。
復制代碼
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });

//如果找到一個 List<T>,其中所有元素均與指定謂詞定義的條件匹配,則為該數組;否則為一個空
var emp = employees.FindAll(e => e.Id >=3);
//遍歷訪問 foreach (var item in emp) { Console.WriteLine(item.Id); Console.WriteLine(item.Name); Console.WriteLine(item.City); Console.WriteLine(item.BirthDate); }
復制代碼
復制代碼
復制代碼
FindIndex(Predicate<T>)    搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中第一個匹配元素的從零開始的索引。
復制代碼
List<Employees> employees = new List<Employees>();
employees.Add(new Employees { Id = 1, Name = "Nancy.Davolio", City = "Seattle", BirthDate = Convert.ToDateTime("1948-12-08") });
employees.Add(new Employees { Id = 2, Name = "Andrew.Fuller", City = "Tacoma", BirthDate = Convert.ToDateTime("1952-02-19") });
employees.Add(new Employees { Id = 3, Name = "Janet.Leverling", City = "Kirkland", BirthDate = Convert.ToDateTime("1963-08-30") });
employees.Add(new Employees { Id = 4, Name = "Margaret.Peacock", City = "Redmond", BirthDate = Convert.ToDateTime("1937-09-19") });
employees.Add(new Employees { Id = 5, Name = "Steven.Buchanan", City = "London", BirthDate = Convert.ToDateTime("1955-03-04") });

//如果找到與 match 定義的條件相匹配的第一個元素,則為該元素的從零開始的索引;否則為 -1
var emp = employees.FindIndex(e => e.Id ==3);
Console.WriteLine("查到的索引為:" + emp);  //2
復制代碼
復制代碼
FindLast(Predicate<T>)    搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中的最后一個匹配元素。
使用方式同FindIndex
IndexOf(T)    搜索指定的對象,並返回整個 List<T> 中第一個匹配項的從零開始的索引
LastIndexOf(T)    搜索指定對象並返回整個 List<T> 中最后一個匹配項的從零開始索引。

https://www.cnblogs.com/liessay/p/12766081.html


免責聲明!

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



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