C#Linq OrderBy(x=>;x.property)
我有一个对给定属性排序的列表C#Linq OrderBy(x=>;x.property),c#,linq,C#,Linq,我有一个对给定属性排序的列表 public class data() { public string attribute1 { get; set; } public string attribute2 { get; set; } public string attribute3 { get; set; } } 但是我想先定义对象,然后对给定对象按顺序执行order。我想知道这是否可能。 例如: _list.OrderBy(x => x.attribute1).ToLis
public class data() {
public string attribute1 { get; set; }
public string attribute2 { get; set; }
public string attribute3 { get; set; }
}
但是我想先定义对象,然后对给定对象按顺序执行order。我想知道这是否可能。
例如:
_list.OrderBy(x => x.attribute1).ToList();
如果需要创建动态order by语句,可以这样做:
object myAttribute = attribute1;
_list.OrderBy(x => x.myAttribute).ToList();
Func orderBy=null;
如果(…)
orderBy=item=>item.attribute1;
其他的
orderBy=item=>item.attribute2;
_list.OrderBy(OrderBy.ToList();
如果需要创建动态order by语句,可以这样做:
object myAttribute = attribute1;
_list.OrderBy(x => x.myAttribute).ToList();
Func orderBy=null;
如果(…)
orderBy=item=>item.attribute1;
其他的
orderBy=item=>item.attribute2;
_list.OrderBy(OrderBy.ToList();
无论你在尝试什么,我想这都是不可能的
但您可以在数据类中定义myAttribute属性。如果它适合您,请尝试此方法
Func<Item, Object> orderBy = null;
if(...)
orderBy = item => item.attribute1 ;
else
orderBy = item => item.attribute2;
_list.OrderBy(orderBy).ToList();
无论你在尝试什么,我想这都是不可能的 但您可以在数据类中定义myAttribute属性。如果它适合您,请尝试此方法
Func<Item, Object> orderBy = null;
if(...)
orderBy = item => item.attribute1 ;
else
orderBy = item => item.attribute2;
_list.OrderBy(orderBy).ToList();
如何获取
\u list
中的对象?list.OrderBy(…).ToList()
效率低下,因为它创建了一个副本,而不是直接调用list.Sort()
,尽管它不使用属性选择器来确定排序依据,您可以提供自己的比较器函数。您是否在寻找查询属性名称的方法,例如string name=attribute1;list.OrderBy(name)
?可能是重复的,这正是我想要的。但是attribute1是一个属性而不是字符串。如何获取\u list
中的对象?list.OrderBy(…).ToList()效率低下,因为它创建了一个副本,而直接调用list.Sort()
,尽管它不使用属性选择器来确定排序的依据,您可以提供自己的比较器函数。您是否在寻找查询属性名称的方法,例如string name=attribute1;list.OrderBy(name)
?可能是重复的,这正是我想要的。但是attribute1是一个属性而不是字符串。