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

发布于 2024-11-13 06:39:10 字数 547 浏览 3 评论 0原文

我有一个绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深白境迁sunset 2024-11-20 06:39:10

您可以使用 Func 作为包含 lambda 表达式的方法参数。

如果您想按 ID 对每种类型进行排序,您也可以为所有这些类型指定一个接口并使用通用方法。

例如,

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

这样调用

可以像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

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.

潜移默化 2024-11-20 06:39:10

您定义一个排序函数,如下所示:(

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

按负 ID 排序与按 ID 降序排序具有相同的效果。)

然后您可以将此排序函数应用于任何 IEnumerable

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

You define a sort function like this:

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

(Sorting by negative ID has the same effect as sorting by ID descending.)

You can then apply this sort function to any IEnumerable<Tag>:

var sortedData = new ObservableCollection<Tag>(data.OrderBy(sortFunc));
素衣风尘叹 2024-11-20 06:39:10

我设法实现这一目标的方法是使用 thekip 的想法,将 Func 传递到函数中,例如

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;
}

The way I managed to achieve this was with thekip's idea of passing a Func into the function e.g.

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;
}
公布 2024-11-20 06:39:10

我认为您正在寻找 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

不再见 2024-11-20 06:39:10

您可以使用 动态 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);

黯然 2024-11-20 06:39:10

您可以使用动态查询。它将在运行时动态地编写 LINQ 语句。

例如:

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

您可以在这里获取:
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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文