orderBy 传入属性的字符串
IEnumerable下面的的OrderBy可以用于集合的排序。
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
他需要传入一个代理,即通过数据返回排序Key的代理。
常见的用法:
var s = new List<Student>();
s.Add(new Student(, "AAA", ));
s.Add(new Student(, "BBB", ));
s.Add(new Student(, "CCC", ));
s.Add(new Student(, "DDD", ));
var query = s.OrderBy(x =>x.Age);
foreach (var item in query)
Console.WriteLine(item);
现在是另一种情况,已知的是Student的属性名称"Age",怎么对s排序呢。
方法是:通过反射获取属性相对应的属性值。
//排序
s.OrderBy(x => GetPropertyValue(x, "Age")) //函数支持
private static object GetPropertyValue(object obj, string property)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
orderBy 传入属性的字符串的更多相关文章
- java前端传入的json字符串保存到表中的方法
表 service_goods_base 字段如下: 传入的json 字符串: servicePictureArray : [{"picServiceUrl": "h ...
- js传入参数为字符串问题
示例: var device_mac="11qweq234ert"; //第一种方式会报错:Onclick SyntaxError: identifier starts immed ...
- IOS开发-UI学习-NSMutableAttributedString(带属性的字符串)的使用
带属性的字符串: NSString *aa = @"hellochinaIloveYou!"; NSMutableAttributedString *mas = [[NSMutab ...
- 无法从其“Checked”属性的字符串表示形式“checked”创建“System.Boolean”类型
如果你要在后台进行设置的话...在<input type="radio" id="dd" name="dd" runat=" ...
- iOS富文本(一)属性化字符串
概述 iOS一些复杂的文本布局一般都是由底层的Core Text来实现的,直到iOS7苹果发布了Text Kit框架,Text Kit能够很简单实现一些复杂的文本样式以及布局,而Text Kit富文本 ...
- C#通过属性名字符串获取、设置对象属性值
之前理工项目从这个博客找到了相对应的方法:C#通过属性名字符串获取.设置对象属性值 https://www.cnblogs.com/willingtolove/p/12198871.html
- Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用
字符串N一定要用“”双引号包含,从test的包含则用单引号 ‘ ’,如果相反,则不能正确判断该属性是否与该字符串相等. 正确:<s:if test='activityBean.searchFor ...
- react 不能往组件中传入属性的值为 undefined
在使用 andt design 的时候遇到个需求,需要清除 Select 组件选中后的值,让它变成什么都没选中,显示 placeholder 刚开始以为设置为 null 即可,结果发现设置为 null ...
- 传入sql数组字符串,输出table
CREATE function [dbo].[split](@aString varchar(),@pattern varchar()) returns @temp table([Sid] [, ) ...
随机推荐
- 使用 MD5 加密 去重对插入的影响
现在有3000条数据,需要插入到数据库中去,使用的是对链接进行MD5加密, hashcode = md5(str(item_url))然后在数据库中设置 hashcode 为UNIQUE索引 3000 ...
- Python 爬虫实例(12)—— python selenium 爬虫
# coding:utf- from common.contest import * def spider(): url = "http://www.salamoyua.com/es/sub ...
- springboot 错误处理
在 java web开发过程中,难免会有一些系统异常或人为产生一些异常.在 RESTful springboot 项目中如何优雅的处理? 分析:在RESTful 风格的springboot 项目中,返 ...
- Fast Algorithm To Find Unique Items in JavaScript Array
When I had the requirement to remove duplicate items from a very large array, I found out that the c ...
- python 元类(metaclass)
元类参见老师的博客 http://www.cnblogs.com/linhaifeng/articles/8029564.html
- Intellij IDEA 导入Eclipse的Web项目
Project Structure -> Modules -> Web (Module使用web技术)
- ssm redis 数据字典在J2EE中的多种应用与实现
数据字典在项目中是不可缺少的“基础设施”,关于数据字典如何设计如何实现,今天抽空讲一下吧 先看一下表设计: 通过自定义标签来实现页面的渲染: public class DataDictValueTag ...
- Atitit 架构的原则attilax总结
Atitit 架构的原则attilax总结 1.1. Rule of three称为"三次原则",指的是当某个功能第三次出现时,才进行"抽象化".是DRY原则和 ...
- sql索引唯一
alter table et_tb_1111 add constraint tbunique unique (itemid) alter table 表名 add constraint 约束名 uni ...
- [SQL in Azure] Tutorial: AlwaysOn Availability Groups in Azure (GUI)
http://msdn.microsoft.com/en-us/library/azure/dn249504.aspx Tutorial: AlwaysOn Availability Groups i ...