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.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