当前位置:网站首页>linq 实现动态 orderby
linq 实现动态 orderby
2022-07-15 17:01:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
class Pet
{
public string Name{get;set;}
public int Age{get;set;}
}
void Main()
{
Pet[] pets = { new Pet { Name="Tim", Age=18 },
new Pet { Name="Allen", Age=22 },
new Pet { Name="Bill", Age=20 } };
//如果我们想根据Age进行排序 很容易想到这样来写:
var query= from p in pets
orderby p.Age
select p;
query.ToList().ForEach(q=>Console.WriteLine(q.Name +" "+q.Age));
/* 得到结果:
Tim 18
Bill 20
Allen 22
*/
}
//但是有时项目内有多个排序条件 如有时要根据Name排序 有时要根据Age排序 何问起 hovertree.com
//这时我们就要用到动态排序:
void Main()
{
Pet[] pets = { new Pet { Name="Tim", Age=18 },
new Pet { Name="Allen", Age=22 },
new Pet { Name="Bill", Age=20 } };
Console.WriteLine("Before Orderby:/r/n");
pets.ToList().ForEach(p=>Console.WriteLine(p.Name +" "+p.Age));
var query= from p in pets
orderby GetPropertyValue(p,"Age")
select p;
Console.WriteLine("/r/nAfter Orderby:/r/n");
query.ToList().ForEach(q=>Console.WriteLine(q.Name +" "+q.Age));
/*
Before Orderby:
Tim 18
Allen 22
Bill 20
After Orderby:
Tim 18
Bill 20
Allen 22
*/
} /* 何问起 hovertree.com */
private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
} 发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120405.html原文链接:https://javaforall.cn
边栏推荐
- Programming exercises
- Domestic light! High score spatiotemporal representation learning model uniformer
- Oscilloscope use concept record
- Compileflow Taobao Workflow Engine
- Insert any element at the specified position in the array and delete the element with value x in the array
- Cron表达式使用
- 免驱 USB 转串口 Billboard 芯片(LDR2001)
- 开源小工具记录
- COMS技术
- 9. MySQL -- JDBC入门
猜你喜欢

C language Chapter 8 array
![[interview must brush 101] hash](/img/01/982c24e923563d74436f91dd4f573e.png)
[interview must brush 101] hash

Lifecycle:生命周期感知型组件的基础 —— Jetpack 系列(1)

Mysql高版本报sql_mode=only_full_group_by异常

Timesformer: can you understand video by transformer alone? Another attack of attention mechanism

compileflow 淘宝工作流引擎

Swin transformer, the best paper model of iccv 2021, finally started video

复杂链表的复制

自增(自减)运算符的运算优先级

传值、传引用、传指针
随机推荐
Mmdetection record of win10 installation
【面试必刷101】双指针
Yotact structure diagram
Ffmpeg sample analysis: muting c
深度学习-损失函数
Lifecycle: the foundation of lifecycle aware components - jetpack series (1)
Thumbnailator 图片处理类库
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
H264-解码顺序 显示顺序 参考顺序
TP5的一些查询
ClickHouse SQL 的十項優化規則
Processing and analysis of yolov3 training data
Exploration of yolact model structure
Compileflow Taobao workflow engine
C language Chapter 9 string
Gridsearchcv reported an error: valueerror: input contains Nan, infinity or a value too large for dtype ('float64 ')
浅析PHP关键词替换的类(避免重复替换,保留与还原原始链接)
枚举,你了解它吗?
Asymmetric encryption RSA and symmetric encryption AES project application
DEBUG系统