使用Object属性作为方法参数

时间:2021-07-31 02:24:24

I have a rather complex database, where we are using Linq to SQL. I'm creating a model layer where I would like to only have one method. But my problem is that we often like to order the collection. Is it possible somehow to accomplish something like this:

我有一个相当复杂的数据库,我们使用Linq to SQL。我正在创建一个模型图层,我希望只有一个方法。但我的问题是我们经常喜欢订购这个系列。有可能以某种方式完成这样的事情:

public static List<Object> GetObject(Object.Property)
{
   return some Linq.ToList();
}

I know I can use linq on my list afterwards.

我知道我之后可以在我的名单上使用linq。

Hmm it was maybe a bit to diffuse question.

嗯,这可能有点分散问题。

OK I solved it with reflection and a string in the argument..

好的,我用反射和参数中的字符串解决了它。

MyObjectDataDataContext context = new MyObjectDataDataContext ();

PropertyInfo[] piArray = context.MyObject.GetType().GetProperties();
PropertyInfo pi = piArray.FirstOrDefault(s => s.Name == "property");
if (pi != null)
{
    return context.MyObject.OrderBy(t => pi.PropertyType);
}

1 个解决方案

#1


1  

I guess you are trying to access the same data, but depending on a 'column' criteria, return the data sorted?

我想您正在尝试访问相同的数据,但根据“列”标准,返回已排序的数据?

Once you have the IEnumerable data, you can sort it as follows:

获得IEnumerable数据后,可以按如下方式对其进行排序:

list.OrderBy(t => t.ColumnYouWantToSortBy)

As in the following documentation

如以下文档中所述

#1


1  

I guess you are trying to access the same data, but depending on a 'column' criteria, return the data sorted?

我想您正在尝试访问相同的数据,但根据“列”标准,返回已排序的数据?

Once you have the IEnumerable data, you can sort it as follows:

获得IEnumerable数据后,可以按如下方式对其进行排序:

list.OrderBy(t => t.ColumnYouWantToSortBy)

As in the following documentation

如以下文档中所述