其他分享
首页 > 其他分享> > LINQ常用操作

LINQ常用操作

作者:互联网

LINQ常用操作:查询,过滤,排序,分组,连接,聚合函数,元素操作符

为了方便了解LINQ的操作,先准备一下测试数据源:

//基础类型
public class Friend
    {
        public int FId { get; set; }
        public string FriendName { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
        public int FTypeId { get; set; }
    }

    public class FriendType
    {
        public int FTypeId { get; set; }
        public string FTypeName { get; set; }
    }

 //朋友信息
    IList<Friend> friendList = new List<Friend>() { 
                         new Friend() { FId = 1, FriendName = "小明", Age = 23 ,Phone="13800008883",FTypeId=1} ,
                         new Friend() { FId = 2, FriendName = "小华", Age = 20 ,Phone="17600008887",FTypeId=2} ,
                         new Friend() { FId = 3, FriendName = "小河", Age = 33 ,Phone="19800008889",FTypeId=1} ,
                         new Friend() { FId = 4, FriendName = "小连", Age = 26 ,Phone="18800008888",FTypeId=2} ,
                         new Friend() { FId = 5, FriendName = "小玲", Age = 22 ,Phone="14800008884",FTypeId=2} ,
                        };

 //朋友类型
    IList<FriendType> fTypeList = new List<FriendType>(){
                           new FriendType(){FTypeId=1,FTypeName="知心人"},
                           new FriendType(){FTypeId=2,FTypeName="好朋友"},
                        };

 

LINQ查询

//查询语法
var friends = from f in friendList
              select new {f.FId,f.FriendName,f.Age};

//Lamdba语法
var friends2 = friendList.Select(f => new { f.FId, f.FriendName,f.Age });

 LINQ过滤

//查询语法
var friends = from f in friendList
                    where f.Age > 20                         
                    select new {f.FId,f.FriendName,f.Age};

//Lamdba语法
var friends2 = friendList.Where(f=>f.Age>20).Select(f => new { f.FId, f.FriendName, f.Age });

LINQ排序(升序、降序)

//查询语法 升序
var friends = from f in friendList
                    orderby f.Age                        
                    select new {f.FId,f.FriendName,f.Age};
//Lamdba语法 升序
var friends2 = friendList.OrderBy(f=>f.Age).Select(f => new { f.FId, f.FriendName, f.Age });

//查询语法 降序
var friends = from f in friendList
                    orderby f.Age descending                       
                    select new {f.FId,f.FriendName,f.Age};
//Lamdba语法 降序
var friends2 = friendList.OrderByDescending(f=>f.Age).Select(f => new { f.FId, f.FriendName, f.Age });

 LINQ分组

var friends = from f in friendList
              group f by f.FTypeId; 

var friends2 = friendList.GroupBy(f => f.FTypeId);

 LINQ连接

//查询语法
var friendTypes = from f in friendList
                              join ft in fTypeList
                              on f.FTypeId equals ft.FTypeId
                              select new
                              {
                                  fName = f.FriendName,
                                  ftName = ft.FTypeName

                              };
//Lamdba语法
var friendTypes2 = friendList.Join(
                fTypeList,
                f => f.FTypeId,
                ft => ft.FTypeId,
                (f, ft) => new
                {
                    fName = f.FriendName,
                    ftName = ft.FTypeName
                });

LINQ聚合函数

//平均值
var avgAge = friendList.Average(u => u.Age);
//总数值
var userCount = friendList.Count();
//最大值
var maxAge = friendList.Max(u => u.Age);
//最小值
var minAge = friendList.Min(u => u.Age);
//总和值
var sumAge = friendList.Sum(u => u.Age);

LING元素操作符

//获取指定索引元素
Friend fri = friendList.ElementAt(0);
//获取最前一个元素
Friend friFirst = friendList.First();
Friend friFirst = friendList.FirstOrDefault();
//获取最后一个元素 
Friend frLast = friendList.Last();
Friend frLast = friendList.LastOrDefault(); 
//获取唯一元素 
Friend friSignal = friendList.Single(u => u.Age < 21);
Friend friSignal = friendList.SingleOrDefault(u => u.Age < 21);

以上元素操作符区别说明:

Frist与FristOrDefault 区别:如果源序列为空,Frist 则抛异常。FristOrDefault 如果序列中不包含任何元素,则返回默认值。

Last与LastOrDefault 区别:如果源序列为空,Last 则抛异常。LastOrDefault 如果序列中不包含任何元素,则返回默认值。

Single与SingleOrDefault 区别:Single 如果该序列并非恰好包含一个元素,则会引发异常。SingleOrDefault 如果这类元素不存在,则返回默认值;如果有多个元素满足该条件,此方法将引发异常。

标签:常用,friendList,FTypeId,LINQ,FriendName,new,var,操作,Age
来源: https://www.cnblogs.com/herojy886/p/13335918.html