gpt4 book ai didi

c# - 扩展 Marc Gravell 的动态 Linq OrderBy

转载 作者:太空狗 更新时间:2023-10-29 20:31:53 25 4
gpt4 key购买 nike

我发现 Marc Gravell 的动态订单很棒:

Dynamic LINQ OrderBy on IEnumerable<T>

我已经把它放在类 LinqHelper 中了。在这个类中,我还创建了两个新类,因此在我的代码中我可以这样做:

var q = db.tblJobHeaders;

LinqHelper.OrderByCollection OBys = new LinqHelper.OrderByCollection();
OBys.AddOrderBy("some field", true);
OBys.AddOrderBy("anotherfield", false);
OBys.ExecuteOrderBys(q);

实现这一目标的类是:

/// <summary>
/// A collection of order bys
/// </summary>
public class OrderByCollection
{
private ArrayList Orderings = new ArrayList();

public OrderByCollection(){ }

/// <summary>
/// Add an order by to this collection
/// </summary>
public void AddOrderBy(string Field, bool Descending)
{
OrderByObj NewObj = new OrderByObj(Descending, Field);
this.Orderings.Add(NewObj);
}

/// <summary>
/// Executes the order bys
/// </summary>
public IOrderedQueryable<T> ExecuteOrderBys<T>(this IOrderedQueryable<T> source)
{
int ExecutionIndex = 0;
foreach (OrderByObj O in this.Orderings)
{
if (ExecutionIndex == 0)
{
if (O.Descending)
source = LinqHelper.OrderByDescending(source, O.Field);
else
source = LinqHelper.OrderBy(source, O.Field);
}
else
{
if (O.Descending)
source = LinqHelper.ThenByDescending(source, O.Field);
else
source = LinqHelper.ThenBy(source, O.Field);
}
ExecutionIndex++;
}
return (IOrderedQueryable<T>)source;
}
}

/// <summary>
/// An order by object
/// </summary>
private class OrderByObj
{
public bool Descending { get; set; }
public string Field { get; set; }

public OrderByObj(bool IsDescending, string DatabaseField)
{
this.Descending = IsDescending;
this.Field = DatabaseField;
}
}

不过我对将 Linq 变量传递给函数还很陌生(这让我有点困惑)。我目前收到以下错误:

OBys.ExecuteOrderBys(q);

这给出了错误:

The type arguments for method 'LinqHelper.OrderByCollection.ExecuteOrderBys(System.Linq.IOrderedQueryable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

如果有人能提供帮助,我对此有点困惑,我是否正确传递了 var q,然后正确返回了它?

最佳答案

我打赌 q 的类型是IQueryable<T>而不是 IOrderedQueryable<T> .只需更改签名就可以了,因为您从 OrderBy 开始.

然后你需要一个IOrderedQueryable<T>对于 ThenBy秒。你可以直接转换它,因为你确定你有一个 IOrderedQueryable<T>从之前调用 OrderByThenBy .

如果您不喜欢类型转换的想法,您需要进行一些更改:

public IOrderedQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)
{
if(!this.Orderings.Any())
throw new InvalidOperationException("You need to add orderings");
IOrderedQueryable<T> ordered;
if (this.Orderings[0].Descending)
ordered = LinqHelper.OrderByDescending(source, this.Orderings[0].Field);
else
ordered = LinqHelper.OrderBy(source, this.Orderings[0].Field);
foreach(var ordering in this.Orderings.Skip(1))
{
if (ordering.Descending)
ordered = LinqHelper.ThenByDescending(source, ordering.Field);
else
ordered = LinqHelper.ThenBy(source, ordering.Field);
}
return ordered;
}

请注意,如果您不添加任何顺序,您的代码将会失败,因为转换为 IOrderedQueryable<T>。到底。您可以将返回类型更改为 IQueryable<T> (它失去了稍后“附加”更多 OrderBys 的能力),或者如果没有订单则抛出,就像我所做的那样。

关于c# - 扩展 Marc Gravell 的动态 Linq OrderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6070500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com