C# 是否可以将 orderby LINQ 子句抽象为函数参数?
我有一个绑定到 WPF 列表视图的 ObservableCollection。我希望能够通过单击列标题对 ListView 控件的列进行排序。为此,我对 ObservableCollection 进行排序,并让绑定负责更新 GUI。
为了对 ObservableCollection 进行排序,我使用以下代码:
sortedData = new ObservableCollection<Tag>( from x in data
orderby x.ID descending
select x );
data = sortedData;
注意:数据绑定到 ListView
我遇到的问题是,对于每一列,都会有大量的复制粘贴代码来实现所需的效果。是否可以将 LINQ 语句的“orderby x.ID 降序”部分作为函数参数传递?
或者有没有一种更简单的方法来达到预期的结果?
I have an ObservableCollection Bound to a WPF List View. I am looking to be able to sort the columns of the ListView control by clicking on the Column Header. To do this I am sorting the ObservableCollection and letting the binding take care of updating the GUI.
To sort the ObservableCollection I am using the following code:
sortedData = new ObservableCollection<Tag>( from x in data
orderby x.ID descending
select x );
data = sortedData;
NB: data is bound to the 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?
Or is there an entirely easier way to achieve the desired result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Func 作为包含 lambda 表达式的方法参数。
如果您想按 ID 对每种类型进行排序,您也可以为所有这些类型指定一个接口并使用通用方法。
例如,
这样调用
可以像
Sort(list.OrderByDescending(x => x.ID));
,其中列表是您的 ObservableCollection。
You could use a Func as a method parameter containing a lambda expression.
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.
For example
which can be called like
Sort(list.OrderByDescending(x => x.ID));
in which list is your ObservableCollection.
您定义一个排序函数,如下所示:(
按负 ID 排序与按 ID 降序排序具有相同的效果。)
然后您可以将此排序函数应用于任何
IEnumerable
:You define a sort function like this:
(Sorting by negative ID has the same effect as sorting by ID descending.)
You can then apply this sort function to any
IEnumerable<Tag>
:我设法实现这一目标的方法是使用 thekip 的想法,将 Func 传递到函数中,例如
The way I managed to achieve this was with thekip's idea of passing a Func into the function e.g.
我认为您正在寻找 Dynamic Linq。
看看这些示例是否有帮助:-
http://msdn.microsoft.com/en-gb/ bb737920.aspx
http://msdn.microsoft.com/en-gb/bb737920.aspx
I think you are looking for Dynamic Linq.
See if these sample helps :-
http://msdn.microsoft.com/en-gb/bb737920.aspx
http://msdn.microsoft.com/en-gb/bb737920.aspx
您可以使用 动态 Linq 为此
,您可以编写如下内容:
var sortExpr = "x.ID";
IQueryable; sortedQuery = query.OrderBy(sortExpr);
You can use Dynamic Linq for this
whith it you can write something like:
var sortExpr = "x.ID";
IQueryable<Tag> sortedQuery = query.OrderBy(sortExpr);
您可以使用动态查询。它将在运行时动态地编写 LINQ 语句。
例如:
您可以在这里获取:
http://code.msdn.microsoft.com/DynamicQuery-f65f6a4d
You can use Dynamic Query.It will composing LINQ statements on the fly, dynamically, at run time.
For example:
You can get it here:
http://code.msdn.microsoft.com/DynamicQuery-f65f6a4d