在List中常用的linq表达式

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

public List<Model.Resume> GetResumeList()
{
      var list = new List<Model.Resume>();
      list.Add(new Model.Resume { Name = "张三", AreaId = 380, AreaPid = 1, SubjectId = 561, SubjectPid = 560, Reg = 1, Price = 0, State = 1 });
      list.Add(new Model.Resume { Name = "李四", AreaId = 381, AreaPid = 1, SubjectId = 562, SubjectPid = 560, Reg = 1, Price = 0, State = 1 });
      list.Add(new Model.Resume { Name = "王五", AreaId = 382, AreaPid = 1, SubjectId = 562, SubjectPid = 560, Reg = 1, Price = 0, State = 1 });
      list.Add(new Model.Resume { Name = "赵六", AreaId = 383, AreaPid = 1, SubjectId = 563, SubjectPid = 560, Reg = 1, Price = 0, State = 1 });
      list.Add(new Model.Resume { Name = "何七", AreaId = 384, AreaPid = 1, SubjectId = 564, SubjectPid = 560, Reg = 1, Price = 0, State = 1 });
      return list;
}
var list = GetResumeList();

 

1.Count(数量)

var num = list.Count;

 

2.FirstOrDefault(取一个对象)

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

 

3.Take(前N条)

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

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

 

4.ForEach(循环)

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

 

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 = { 561, 562 }; 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

posted @ 2015-07-23 13:26  paulhe  阅读(1825)  评论(0编辑  收藏  举报
friendster counter