本文介绍了C#Linq OrderBy(x => x.property)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class data() {
   public string attribute1 { get; set; }
   public string attribute2 { get; set; }
   public string attribute3 { get; set; }
}

我有一个订购给定属性的列表.

I have a list that orders the given attribute.

_list.OrderBy(x => x.attribute1).ToList();

但是我想先定义对象,然后再按赋予对象的顺序执行命令.我想知道这是否可能.例如:

But I want to define the object first and then execute order in order with the give object. I am wondering if this is possible.for example:

object myAttribute = attribute1;
_list.OrderBy(x => x.myAttribute).ToList();

推荐答案

如果您需要通过语句创建动态订单,则可以这样做:

If you need to create dynamic order by statements, you can do it like this:

Func<Item, Object> orderBy = null;

if(...)
   orderBy = item => item.attribute1 ;
else 
  orderBy = item => item.attribute2;

_list.OrderBy(orderBy).ToList();

这篇关于C#Linq OrderBy(x => x.property)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:17