博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#.net工作笔记001---Linq对象查询,排序,分组,去重在工作中的使用_随时更新
阅读量:1893 次
发布时间:2019-04-26

本文共 3006 字,大约阅读时间需要 10 分钟。

 

1.对list中的某两个字段按照升序排序

 testlist=testlist.OrderBy(s=>new{s.cd1,s.cd2}).ToList<TestDto>();

List<Student> stu = (List<Student>)Session["StudentList"];

下面是详细一点的排序:

Linq表达式:

//按学号降序

List<Student> stuList = (from s instu orderby s.stuNOdescending select s).ToList<Student>();

 

//按学号升序

List<Student> stuList = (from s instu orderby s.stuNO  select s).ToList<Student>();

 

使用Lambda表达式排序: //按学号降序

单字段:List<Student> stuList= stu.OrderByDescending(s=> s.orderid).ToList<Student>();
多字段:List<Student> stuList= stu.OrderByDescending(s=> new{s.stuNO,s.stuName}).ToList<Student>();
//按学号升序
单字段:List<Student> stuList= stu.OrderBy(s=> s.stuNO).ToList<Student>();
多字段:List<Student> stuList= stu.OrderBy(s=> new{s.stuNO,s.stuName}).ToList<Student>();
多字段主次顺序排序情况,先按no排序,再按name排序
List<Student> stuList= stu.OrderBy(s=> s.stuNO).ThenBy(s=> s.stuName).ToList<Student>();
List<Student> stuList= stu.OrderBy(s=> new{ s.stuNO }).ThenBy(s=> new{s.stuName}).ToList<Student>();

//2.先排序再分组

testlist =(List<TestDto>)(from m in testlist orderby m.orderno group m by new {m.nroderno , m.ordernodtl} into mygroup select mygroup);

//3.获取一个list中的某个字段最大的值

(from c in biaoming where c.名称== 条件 select  c.ManualID).Max();

//4.按照条件查询,先查询再排序

 LstTmp = (from m in TmpLst where (m.testcd == "1") && (m.testcd2 == "2") select m).ToList<TestDto>().OrderBy(s => new { s.sor1, s.sort2 }).ToList<TestDto>();

//5.

问题描述

比如有如下实体集合:

Person1: Id=1, Name="Test1"Person2: Id=1, Name="Test1"Person3: Id=2, Name="Test2"

如何使用LINQ按 Person.Id 去重,返回的集合只包含 Person1 和 Person3 ?

方案一

创建一个静态扩展类:这个主意一下,这个可以放到一个命名空间下,然后再写一个类:

像下面这样,这样才能用:

static class ListExtendMethod    {           public static IEnumerable
DistinctBy
(this IEnumerable
source, Func
keySelector) { HashSet
seenKeys = new HashSet
(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } } }

 

public static IEnumerable
DistinctBy
(this IEnumerable
source, Func
keySelector){ HashSet
seenKeys = new HashSet
(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } }}

调用方法:

var query = people.DistinctBy(p => p.Id);

如果需要按多个属性去重,则可以使用匿名对象,如:

var query = people.DistinctBy(p => new { p.Id, p.Name });

方案二

List
distinctPeople = allPeople .GroupBy(p => p.PersonId) .Select(g => g.First()) .ToList();

多属性去重:

List
distinctPeople = allPeople .GroupBy(p => new {p.PersonId, p.FavoriteColor} ) .Select(g => g.First()) .ToList();

方案三

var uniquePeople = from p in people                   group p by new {p.ID} //or group by new {p.ID, p.Name, p.Whatever}                   into mygroup                   select mygroup.FirstOrDefault();

方案四

Persons.ToLookup(p => p.Id).Select(coll => coll.First());

方案五

var result = people.Where(p => !people.Any(q => (p != q && p.Id == q.Id)));

转载地址:http://fltdf.baihongyu.com/

你可能感兴趣的文章
NFS使用方法详解
查看>>
LINUX下的设备驱动程序
查看>>
_IO, _IOR, _IOW, _IOWR 宏的用法与解析
查看>>
做DSP最应该懂得157个问题
查看>>
cp: cannot stat '/usr/local/bin/node': Too many levels of symbolic links
查看>>
Jenkins 连接github
查看>>
android中是Aspect 进行埋点笔记
查看>>
git 使用报错: fatal: Couldn‘t find remote ref master的解决方法
查看>>
php 魔术方法 __invoke()
查看>>
php_sapi_name
查看>>
Vue 节流(throttle)和防抖(debounce)
查看>>
小程序初始页面地址修改
查看>>
微信小程序使用腾讯地图
查看>>
http-server的安装和使用(vue 打包后项目运行)
查看>>
vuex中使用typescript
查看>>
vue 使用 typescript ref中的方法找不到(uniapp 中使用 typescript ref)
查看>>
vue使用typescript / uniapp 使用typescript
查看>>
uniapp 使用typescript 运行小程序正常 h5 报错/ keepAliveInclude is not defined
查看>>
webpack打包html里面img后src为“[object Module]”
查看>>
使用webpack打包发现css单行注释报错
查看>>