简体   繁体   English

C# 是否可以通过 LINQ 子句将 orderby 抽象为 function 参数?

[英]C# Is it possible to abstract an orderby LINQ clause into a function parameter?

I have an ObservableCollection Bound to a WPF List View.我有一个绑定到 WPF 列表视图的 ObservableCollection。 I am looking to be able to sort the columns of the ListView control by clicking on the Column Header.我希望能够通过单击列 Header 对 ListView 控件的列进行排序。 To do this I am sorting the ObservableCollection and letting the binding take care of updating the GUI.为此,我对 ObservableCollection 进行排序并让绑定负责更新 GUI。

To sort the ObservableCollection I am using the following code:要对 ObservableCollection 进行排序,我使用以下代码:

sortedData = new ObservableCollection<Tag>( from x in data
                                            orderby x.ID descending
                                            select x );
data = sortedData;

NB: data is bound to the ListView注意:数据绑定到 ListView

The problem I'm having is that for each of the columns there would be a lot of copy-paste code to achieve the desired effect.我遇到的问题是,对于每一列,都会有很多复制粘贴代码来达到预期的效果。 Is it possible to pass the 'orderby x.ID descending' portion of the LINQ statement as a function parameter?是否可以将 LINQ 语句的“orderby x.ID 降序”部分作为 function 参数传递?

Or is there an entirely easier way to achieve the desired result?或者有没有更简单的方法来达到预期的结果?

You could use a Func as a method parameter containing a lambda expression.您可以使用 Func 作为包含 lambda 表达式的方法参数。

If you want to order every single type by it's ID you could specify an interface on all of those types too and use a generic method.如果您想按其 ID 对每种类型进行排序,您也可以在所有这些类型上指定一个接口并使用泛型方法。

For example例如

public ObservableCollection<Tag> Sort(Func<ObservableCollection<Tag>> sortFunc)  
{  
    //do something else  
    data = sortFunc();  
    //do something else  
}

which can be called like可以称为

Sort(list.OrderByDescending(x => x.ID));

in which list is your ObservableCollection.其中列表是您的 ObservableCollection。

You define a sort function like this:您像这样定义一个排序 function

Func<Tag, int> sortFunc = x => -x.ID;

(Sorting by negative ID has the same effect as sorting by ID descending.) (按负 ID 排序与按 ID 降序排序的效果相同。)

You can then apply this sort function to any IEnumerable<Tag> :然后,您可以将此排序 function 应用于任何IEnumerable<Tag>

var sortedData = new ObservableCollection<Tag>(data.OrderBy(sortFunc));

The way I managed to achieve this was with thekip's idea of passing a Func into the function eg我设法实现这一点的方法是使用 thekip 将 Func 传递到 function 的想法,例如

sortColumn( "ID", x => x.ID );

protected void sortColumn<T>( string name, Func<Tag, T> selector )
{
    ObservableCollection<Tag> sortedData = new ObservableCollection<Tag>( TagData.OrderBy( selector ) );

    data = sortedData;
}

You can use Dynamic Query.It will composing LINQ statements on the fly, dynamically, at run time.您可以使用 Dynamic Query。它将在运行时动态、动态地编写 LINQ 语句。

For example:例如:

var query =
           db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
           OrderBy("CompanyName").
           Select("New(CompanyName as Name, Phone)");

You can get it here: http://code.msdn.microsoft.com/DynamicQuery-f65f6a4d你可以在这里得到它: http://code.msdn.microsoft.com/DynamicQuery-f65f6a4d

I think you are looking for Dynamic Linq.我认为您正在寻找动态 Linq。

See if these sample helps:-看看这些样本是否有帮助:-

http://msdn.microsoft.com/en-gb/bb737920.aspx http://msdn.microsoft.com/en-gb/bb737920.aspx

http://msdn.microsoft.com/en-gb/bb737920.aspx http://msdn.microsoft.com/en-gb/bb737920.aspx

You can use Dynamic Linq for this您可以为此使用动态 Linq

whith it you can write something like:有了它,您可以编写如下内容:

var sortExpr = "x.ID";

IQueryable<Tag> sortedQuery = query.OrderBy(sortExpr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM