- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试添加 ThenById()
方法,该方法将在调用 IOrderedQueryable
上的 OrderBy()
后启动:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(this IQueryable<TEntity> source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
var command = "ThenBy";
var thenByProperty = "Id";
var type = typeof(TEntity);
if (type.GetProperty(thenByProperty) == null)
{
throw new MissingFieldException(nameof(thenByProperty));
}
var param = Expression.Parameter(type, "p");
var property = type.GetProperty(thenByProperty, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var propertyAccess = Expression.MakeMemberAccess(param, property);
var orderByExpression = Expression.Lambda(propertyAccess, param);
var resultExpression = Expression.Call(
typeof(IOrderedQueryable),
command,
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));
return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery<TEntity>(resultExpression);
}
我收到以下错误消息:
No generic method 'ThenBy' on type 'System.Linq.IOrderedQueryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
最佳答案
ThenBy
扩展方法在 System.Linq.Queryable
类中,而不是在 IOrderedQueryable
中。您只需要在您的代码中替换它:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(
this IOrderedQueryable<TEntity> source)
{
//snip
var resultExpression = Expression.Call(
typeof(System.Linq.Queryable),
command,
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExpression));
return (IOrderedQueryable<TEntity>)source.Provider.CreateQuery<TEntity>(resultExpression);
}
请注意,该方法应该扩展 IOrderedQueryable
,而不仅仅是 IQueryable
。
但是,如果 TEntity
没有 Id
属性,这将在运行时失败。我的偏好是为所有具有 Id
属性的实体提供一个接口(interface),并在此处使用通用约束。这样你就可以完全避免表达式并获得编译时安全性。例如:
public interface IHasId
{
int Id { get; set; }
}
public class SomeEntity : IHasId
{
public int Id { get; set; }
public string Name { get; set; }
//etc
}
这简化了您的扩展方法:
public static IOrderedQueryable<TEntity> ThenById<TEntity>(
this IOrderedQueryable<TEntity> source)
where TEntity : IHasId
{
return source.ThenBy(e => e.Id);
}
关于c# - 没有泛型方法 'ThenBy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55283329/
无法理解为什么我的简单订购没有按预期工作。这是有问题的行: return messages.OrderBy(o => o.MessageType) .ThenBy(p =
我有以下方法: public List defaultSortExpressions { get; set; } private IQueryable orderEntries(IQueryable
我正在尝试添加 ThenById() 方法,该方法将在调用 IOrderedQueryable 上的 OrderBy() 后启动: public static IOrderedQueryable Th
我有一个查询应该这样排序: var list = new List(); var orderedList = list .OrderByDescending(x => x.Project)
灵感来自 this我做了这个: ISortable public interface ISortable { IPageable OrderBy(Expression> orderBy);
我正在使用以下查询语法 from table where where orderby orderby 其中第一个 orderby 是一个日期,第二个 orderby 是一个日期。我认为这将像 orde
谁能解释一下两者之间的区别: tmp = invoices.InvoiceCollection .OrderBy(sort1 => sort1.InvoiceOwner.L
我正在构建一个 Spec Evaluator,它必须考虑多个可能的 OrderBy,如下例所示: if (spec.Order != null) { var count = spec.Orde
我必须编写 Linq 的“立即”模式实现(由于 Unity/Mono 的内存分配限制 - 长话短说,并不重要)。 在我来到 ThenBy 之前,我对所有性能与真正的 Linq 一样快或更快的东西感到满
我正在尝试对绑定(bind)到多个属性的 View 模型进行排序。问题是第二个属性可能为空,我得到一个空引用异常。 return this.People .OrderBy(x => x.Car.N
我有一个包含 50 个元素的点列表,我想对它们进行排序,所以我使用了 orderby thenby to,但我的输出似乎是错误的。第一个元素相应地排序,但接下来的元素是错误的,这是一个屏幕截图。 正确
F# 中是否有任何类似于 LINQ fluent 语法的函数用于按多个表达式排序: myList.OrderBy(fun x->x.Something).ThenBy(fun x->x.Somethi
我正在使用 .NET 4.5,我想了解以下内容: 如果我执行此操作,一切都会按预期进行: Dim lst = Enumerable.Range(1, 10) Dim lstOrdered =
我有一个带有 OrderBy 和 ThenBy 的 linq 查询(与数据库无关) var sortedList = unsortedList .OrderBy(foo
我正在尝试根据先前排好序的列对表格的多个列进行排序。它适用于前两列。但是,一旦我对第三列进行排序,第二列就会失去排序。据我所知,我的 foreach 循环一定有问题。这是我的排序代码: public
我正在尝试按日期对列表进行排序,然后按描述名称对列表进行排序,但是我需要所有具有特定描述的元素成为每个日期的顶部元素。 示例: 01-02-2014 "Description A" 01-02-201
我正在将我的一个项目从使用基于 SQL Server 的 Entity Framework 模型转换为使用本地 SQLite 数据库。到目前为止一切顺利,但由于某种原因我无法对多列查询进行排序。例如:
我有一个独特的情况,我试图在 IQueryable 上执行 .OrderBy。 (如果我的定义不准确,请原谅我) 我有以下内容: IQueryable data = e.Sources.Where(e
我正在 Lua 中实现 LINQ 克隆,但这在这里不太相关,而且我已经完成了大部分功能(可枚举/可查询,还不是预编译器),但想不出一个聪明的实现方式OrderBy 的 ThenBy。 目前我排序一次,
我在我的项目中使用 Linq 和 Lambda 操作,我需要根据类的两个属性对列表进行排序。因此,我使用了 OrderBy().ThenBy() 方法如下: class ValueWithIndex{
我是一名优秀的程序员,十分优秀!