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集合 bJdUserName.DataSource = list.Distinct().ToList(); //去重复,绑定数据后⾯要加ToList()
int tem2 = list.FindLastIndex(x => x == "Jack");//通过lambda-expressions // Display the first structure
List
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)distinct和distinctive
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> 中最后⼀个匹配项的从零开始索引。
wwwblogs/liessay/p/12766081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论