在List中常用的linq表达式

时间:2022-05-18 00:07:26

  为了下面举例方便,先声明一个集合:

public List<Model.Resume> GetResumeList()
{
var list = new List<Model.Resume>();
list.Add(new Model.Resume { Name = "张三", AreaId = , AreaPid = , SubjectId = , SubjectPid = , Reg = , Price = , State = });
list.Add(new Model.Resume { Name = "李四", AreaId = , AreaPid = , SubjectId = , SubjectPid = , Reg = , Price = , State = });
list.Add(new Model.Resume { Name = "王五", AreaId = , AreaPid = , SubjectId = , SubjectPid = , Reg = , Price = , State = });
list.Add(new Model.Resume { Name = "赵六", AreaId = , AreaPid = , SubjectId = , SubjectPid = , Reg = , Price = , State = });
list.Add(new Model.Resume { Name = "何七", AreaId = , AreaPid = , SubjectId = , SubjectPid = , Reg = , Price = , State = });
return list;
}
var list = GetResumeList();

1.Count(数量)

var num = list.Count;

2.FirstOrDefault(取一个对象)

var resume = list.FirstOrDefault(m => m.AreaId == );

3.Take(前N条)

//取前3条
var prevList = list.Take().ToList();
//跳过前3条,取剩余条数
var nextList = list.Skip().ToList();

可应用在按照指定顺序排列分类信息栏目。

4.ForEach(循环)

list.ForEach(m =>
{
m.Name = "张三";
m.State = ;
if (m.SubjectId == )
{
m.Reg = ;
}
});

5.Select(取想要得到的属性)

var newList = list.Select(m => new { m.Name, m.AreaId }).ToList();

可应用在返回指定的json对象。

6.Contains(包含)

var resume = list.FirstOrDefault(m => m.Name.Contains("张"));

7.Contains(in的用法)

//in
int[] arrSubId = { , };
list = list.Where(m => arrSubId.Contains(m.SubjectId)).ToList(); //not in
int[] arrSubId = { 561, 562 };

list = list.Where(m => !arrSubId.Contains(m.SubjectId)).ToList();

8.OrderBy(排序)

http://www.cnblogs.com/paulhe/p/3625397.html

相关文章