Linq 基于排序逻辑的传递属性反转IQueryable
我正在根据传递给升序或降序方法的参数实现排序Linq 基于排序逻辑的传递属性反转IQueryable,linq,Linq,我正在根据传递给升序或降序方法的参数实现排序 else if (showGrid.Sortdir == "DESC") { alerts = DB.Incidents.OfType<Alert>().Where( a => a.IncidentStatusID == (int)
else if (showGrid.Sortdir == "DESC")
{
alerts = DB.Incidents.OfType<Alert>().Where(
a =>
a.IncidentStatusID == (int)AlertStatusType.New ||
a.IncidentStatusID == (int)AlertStatusType.Assigned ||
a.IncidentStatusID == (int)AlertStatusType.Watching)
.OrderByDescending(a => showGrid.Sort);
}
else
{
alerts = DB.Incidents.OfType<Alert>().Where(
a =>
a.IncidentStatusID == (int)AlertStatusType.New ||
a.IncidentStatusID == (int)AlertStatusType.Assigned ||
a.IncidentStatusID == (int)AlertStatusType.Watching)
.OrderBy(a => showGrid.Sort);
}
else if(showGrid.Sortdir==“DESC”)
{
警报=DB.incents.OfType()。其中(
a=>
a、 IncidentStatusID==(int)AlertStatusType.New||
a、 IncidentStatusID==(int)AlertStatusType.Assigned||
a、 IncidentStatusID==(int)AlertStatusType.Watching)
.OrderByDescending(a=>showGrid.Sort);
}
其他的
{
警报=DB.incents.OfType()。其中(
a=>
a、 IncidentStatusID==(int)AlertStatusType.New||
a、 IncidentStatusID==(int)AlertStatusType.Assigned||
a、 IncidentStatusID==(int)AlertStatusType.Watching)
.OrderBy(a=>showGrid.Sort);
}
在升序排序的情况下,它可以正常工作,但降序排序不起作用。我调试了代码,发现列表与升序不同。请帮帮我好的。我写了一个小测试。这很有趣,但您的代码实际上可以编译和工作,但与您所期望的非常不同:) 显然,
showGrid
不是Alert
类型,它是另一类的实例,顺便说一句,与Alert
具有相同的属性,称为Sort。
首先,我感到困惑,因为我认为这段代码无法编译
// The signature of OrderBy
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector)
// In your case it will result in
public static IOrderedQueryable<Alert> OrderBy<Alert, string>(this IQueryable<Alert> source, Expression<Func<Alert, string>> keySelector)
//when you call it like you do
DB.Incidents.OfType<Alert>().OrderByDescending(a => showGrid.Sort);
// You supply a property from object of type different from your entity.
// This is incorrect usage, the only object you can use here is the
// "a" argument. Like this:
DB.Incidents.OfType<Alert>().OrderByDescending(a => a.Sort);
// Because anything else does not make any sense to entity provider.
//OrderBy的签名
公共静态IOrderedQueryable OrderBy(此IQueryable源、表达式键选择器)
//在你的情况下,这将导致
公共静态IOrderedQueryable OrderBy(此IQueryable源、表达式键选择器)
//当你这样称呼它的时候
DB.incents.OfType().OrderByDescending(a=>showGrid.Sort);
//您可以从类型不同于实体的对象提供属性。
//这是不正确的用法,这里唯一可以使用的对象是
//“a”论点。这样地:
DB.incents.OfType().OrderByDescending(a=>a.Sort);
//因为其他任何东西对实体提供者都没有任何意义。
所以你的订单根本不起作用
据我所知,您想要的是根据UI中的选择执行排序。这在强类型LINQ中不容易实现。因为正如我上面所展示的,您向OrderBy发送的是一个属性,而不是一个值。它不关心道具内部的值。因此,有几种解决方案:
以下是我根据用户选择进行排序的解决方案: 创建基本查询
var query = DB.Incidents.OfType<Alert>.Where(
a =>
a.IncidentStatusID == (int)AlertStatusType.New ||
a.IncidentStatusID == (int)AlertStatusType.Assigned ||
a.IncidentStatusID == (int)AlertStatusType.Watching);
听起来好像Sortdir不完全是“DESC”。我建议您将日志记录放在两个分支中(或在代码中进行调试),以查看实际采用的路径。我已经厌倦了它使用“ASC”和“DESC”各自的路径。在调试时,我对“警报”值进行了监视,我可以看到,当它执行OrderByDesending(a=>showGrid.Sort)时,这些项不会受到尊重;什么是
showGrid.Sort
?您首先使用哪种LINQ提供程序?ShowGrid.Sort是一种字符串类型,它返回属性名称,如“AlertNumber”、“IncidentStatusID”等。我希望根据该字符串对“alert”列表进行排序。我使用的是System.Linq版本:v4.0.0.0和运行时版本:v4.0.30319排序无法按您的要求工作。当您发送a=>showGrid.Sort
时,它将把Sort
作为要排序的属性。看起来,您的警报
对象碰巧具有这样的属性。这就是为什么你甚至要编译项目,这是不正确的<Queryable
支持类型为的code>,为了获取特定的派生实体,它不会迭代集合。好的,我已更正。我必须测试这个来说服自己,但是现在没有时间。相应地更改了答案。以下是来自可查询源的引用:类型的公共静态IQueryable(此IQueryable源)
,因此您可以确定:)
bool desc = showGrid.SortDir = "DESC";
switch(showGrid.Sort)
{
case "col1":
query = desc ? query.OrderByDescending( a => a.Col1 ) : query.OrderBy( a => a.Col1 );
break;
case "col2":
query = desc ? query.OrderByDescending( a => a.Col2 ) : query.OrderBy( a => a.Col2 );
break;
...
}
var results = query.ToList();