list数据添加条件
//将provice=吉林的筛选出来。
list = list.Where(x => x.provice == "吉林").ToList();
//将包含1,5条件的数据筛选出来,相当于sql里的in用法:select * from 表 where user_type in (1,5)
list= list.Where(a => "1,5".Contains(a.user_type)).ToList();
//此处等同于上面
list= list.Where(a => a.user_type == "1" || a.user_type == "5").ToList();
//另一种形式,from开头
IList<Model> query = (from item in list
where ("," + projectmodel.ids + ",").Contains("," + item.id + ",")
select item).ToList<Model>();
取出list数组,并对数组分割
string[] id = list.Where(a => !string.IsNullOrEmpty(a.user_type)).Select(a => a.id).ToArray();
//ids="1,2,3,4,5,6,7";
string ids = string.Join(",", id);
左连接和右连接
/dt,dt1,dt2都为DataTable类型。
//左联比内联,需要多写into,需要多写from gc1 in corr.DefaultIfEmpty(),需要多写corr=gc1。
//当on为单条件时。
var results = from student in dt.AsEnumerable()
join user in dt1.AsEnumerable() on student.Field<int>("student_id") equals user.Field<int>("id")//内联
join corr in dt2.AsEnumerable() on student.Field<int>("id") equals corr.Field<int?>("studentproject_id") into corr//左联
from gc1 in corr.DefaultIfEmpty()
select new
{
student,
user,
corr = gc1
};
//当on为多条件时,借助于匿名类型:其实和解决按多条件分组的思路是一样的。
var results = from student in dt.AsEnumerable()
join zrs in result_zrs on new { districtcounty = student.Field<string>("districtcounty"), school = student.Field<string>("school") }
equals new { districtcounty = zrs.districtcounty, school = zrs.school } into zrs
from gc1 in zrs.DefaultIfEmpty()
select new
{
student,
corr = gc1
};
//取值方式
foreach (var i in results.ToList())
{
name = i.user.Field<string>("name");
}
list分页
//Skip是起始数据,表示从第n+1条数据开始.(此处pageNum应从0开始)
//pageNum:页数、=0是第一页,pageSize:一页多少条
list = list.Skip(pageNum * pageSize).Take(pageSize).ToList();
//取前1-10条
list = list.Skip(0).Take(10).ToList();
//也可以这么写取前1-10条
list = list.Take(10).ToList();
//取第11-20条
list = list.Skip(10).Take(10).ToList();
Distinct去重
//字符串数组
string[] idlist = new string[ ]{"aaa","bbb","aaa"};
//去除重复aaa
idlist = idlist.Distinct().ToArray();