- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 .NET 4.5,我想了解以下内容:
如果我执行此操作,一切都会按预期进行:
Dim lst = Enumerable.Range(1, 10)
Dim lstOrdered = lst.OrderBy(Function(i) i Mod 2)
Dim lst2 = lstOrdered.ThenBy(Function(i) -i)
但是,以下代码引发了 System.MissingMemberException
Dim lst = Enumerable.Range(1, 10)
Dim lstOrdered
lstOrdered = lst.OrderBy(Function(i) i Mod 2)
Dim lst2 = lstOrdered.ThenBy(Function(i) -i)
在实际代码中,我们必须有时动态应用 OrderBy,有时动态应用 OrderByDescending,然后动态地继续使用 ThenBy 和 ThenByDescending。所以我想先声明一下。
另请注意,生产代码包含匿名对象而不是整数。
编辑
以下代码更接近生产,我似乎无法适应所提供的答案。它编译但抛出执行错误。
Dim lst = Enumerable.Range(1, 10).Select(Function(i) New With {.a = "foo", .b = i Mod 2, .c = -i})
Dim lstOrdered As IOrderedEnumerable(Of Object)
lstOrdered = lst.OrderBy(Function(i) i.b)
Dim lst2 = lstOrdered.ThenBy(Function(i) i.c)
错误如下
Unable to cast object of type 'System.Linq.OrderedEnumerable`2[VB$AnonymousType_3`3[System.String,System.Int32,System.Int32],System.Int32]' to type 'System.Linq.IOrderedEnumerable`1[System.Object]'
最佳答案
在第二个示例中,您没有声明 lstOrdered
的类型。如果将第二行更改为
Dim lstOrdered As IOrderedEnumerable(Of Integer)
设置Option Strict On
以在编译时捕获这些错误。您可以将 Option Strict On
语句设置为代码文件中的第一个语句,并在“测试|选项|环境|项目和解决方案|VB 默认值”中将该选项设置为默认值。
关于vb.net - 为什么 thenBy 在以下情况下会引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45941947/
无法理解为什么我的简单订购没有按预期工作。这是有问题的行: 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{
我是一名优秀的程序员,十分优秀!