我如何排序依据,并根据用户的输入ThenBy在LINQ? [英] How do I OrderBy and ThenBy in LINQ based on user input?

查看:113
本文介绍了我如何排序依据,并根据用户的输入ThenBy在LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想在同一时间多个列排序页面上的一个网格

I have a grid on a page which I want to be sortable on multiple columns at the same time.

例如:

UserID        FirstName        LastName
=======================================
1             Bruce            Wayne
2             Peter            Parker
3             Clark            Kent
4             Tony             Stark
5             Helena           Wayne

,用户可以选择按姓氏ASC然后通过名字DESC命令,这将产生以下内容:

The user can choose to order by LastName ASC then by FirstName DESC, which would produce the following:

UserID        FirstName        LastName
=======================================
3             Clark            Kent
2             Peter            Parker
4             Tony             Stark
5             Helena           Wayne
1             Bruce            Wayne

用户可以重新排序,并决定将其订购的一些其他的方式。

The user could reset the ordering and decide to order it in some other fashion.

如何在LINQ实现这一目标?据我所知,顺便链排序是做这样的事情。

How can I achieve this in LINQ? As far as I know, the way to chain ordering is to do something like

superheroes.OrderBy(x => x.LastName).ThenByDescending(x => x.FirstName)

显然,我不想写出来,列订单,每一个可能的组合,(我的网格最多可有10列)。有没有一些方法,使排序顺序动态?

Obviously, I don't want to have to write out every possible combination of column orders (my grid may have up to 10 columns). Is there some way to make the ordering sequence dynamic?

推荐答案

您需要使用动态LINQ。看一看的动态LINQ(第一部分:使用LINQ动态查询库)

You need to use dynamic LINQ. Have a look at Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

这篇关于我如何排序依据,并根据用户的输入ThenBy在LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆