Program类里面的代码,实现条件筛选、条件排序。封装UserInfo类,进行调用。
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace Demo01
{
public class UserInfo
{
public int UserId { get ; set ; }
public string UserName { get ; set ; }
public int UserAge { get ; set ; }
public string UserSex { get ; set ; }
public string Address { get ; set ; }
public string Phone { get ; set ; }
public string Email { get ; set ; }
public override string ToString ( )
{
return $"用户编号:{UserId},用户姓名:{UserName},性别:{UserSex},年龄:{UserAge},地址:{Address},联系电话:{Phone},邮箱:{Email}" ;
}
}
}
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace Demo01
{
class Program
{
private static List< UserInfo> userInfos;
private static List< StuScore> stuScores;
static void Main ( string [ ] args)
{
InitData ( ) ;
Exe01 ( ) ;
Exe02 ( ) ;
Console. ReadKey ( ) ;
}
private static void Exe01 ( )
{
var users = from user in userInfos where user. UserSex == "女" select user;
foreach ( var item in users)
{
Console. WriteLine ( item. ToString ( ) ) ;
}
Console. WriteLine ( "-----------------------------------------------" ) ;
var users1 = from user in userInfos orderby user. UserAge descending select user;
foreach ( var item in users1)
{
Console. WriteLine ( item. ToString ( ) ) ;
}
}
private static void Exe02 ( )
{
var users = userInfos. Where ( user = > user. UserAge >= 20 ) . ToList ( ) ;
foreach ( var item in users)
{
Console. WriteLine ( item. ToString ( ) ) ;
}
}
private static void InitData ( )
{
userInfos = new List < UserInfo > ( )
{
new UserInfo ( )
{
UserId = 1 ,
UserName = "张三三" ,
UserAge = 20 ,
Address = "ASFsfdsdkfjlsk" ,
UserSex = "男" ,
Phone = "12324325443" ,
Email = "123@123.123"
} ,
new UserInfo ( )
{
UserId = 2 ,
UserName = "张三丰" ,
UserAge = 120 ,
Address = "武当山" ,
UserSex = "男" ,
Phone = "12324325443" ,
Email = "123@123.123"
} ,
new UserInfo ( )
{
UserId = 3 ,
UserName = "张翠山" ,
UserAge = 40 ,
Address = "武当山" ,
UserSex = "男" ,
Phone = "12324325443" ,
Email = "123@123.123"
} ,
} ;
}
}
}
Program类里面的代码,实现Select进行投影操作。封装StuScore类,进行调用。
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace Demo01
{
public class StuScore
{
public string Name { get ; set ; }
public bool IsWen { get ; set ; }
public double Shuxu { get ; set ; }
public double Yuwen { get ; set ; }
public double Yingyu { get ; set ; }
public double Zonghe { get ; set ; }
public override string ToString ( )
{
return $"姓名:{Name},是否文科:{IsWen},数学:{Shuxu},语文:{Yuwen},英语:{Yingyu},综合:{Zonghe}" ;
}
}
}
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace Demo01
{
class Program
{
private static List< UserInfo> userInfos;
private static List< StuScore> stuScores;
static void Main ( string [ ] args)
{
InitScore ( ) ;
Exe03 ( ) ;
Console. ReadKey ( ) ;
}
private static void Exe03 ( )
{
var data = stuScores. Where ( s = > s. IsWen) . Select ( s = > new { s. Name, Sum = s. Shuxu + s. Yuwen + s. Yingyu + s. Zonghe } ) . ToList ( ) ;
foreach ( var item in data)
{
Console. WriteLine ( item) ;
}
Console. WriteLine ( "平均成绩:" + data. Average ( s = > s. Sum) ) ;
}
private static void InitScore ( )
{
stuScores = new List < StuScore > ( )
{
new StuScore ( )
{
Name = "张三" ,
IsWen = true ,
Shuxu = 49 ,
Yuwen = 121 ,
Yingyu = 110 ,
Zonghe = 180
} ,
new StuScore ( )
{
Name = "李四" ,
IsWen = false ,
Shuxu = 149 ,
Yuwen = 91 ,
Yingyu = 100 ,
Zonghe = 210
} ,
} ;
}
}
}
小剧场:负重一万斤长大。Grow up with a load of ten thousand jin.