简体   繁体   English

混合使用 DateTime 和 DBNull 值时的 OrderBy 会引发错误

[英]OrderBy on mix of DateTime and DBNull values throws error

I want to order a list of my 'SortObject' class.我想订购我的“SortObject”class 列表。 This class is meant to imitate a DataGrid Row by holding arbitrary data organized in a dictionary (named 'Cells'), where the key is analogous to a DataGrid Column.这个 class 旨在通过保存在字典(名为“单元格”)中组织的任意数据来模拟DataGrid Row,其中的键类似于 DataGrid 列。 It is enforced that any given key is associated with only one data type, for example the key "Name" will only have values of the String type.强制任何给定键仅与一种数据类型相关联,例如键“名称”将仅具有字符串类型的值。

My code (below) actually works for a majority of the cases I've used:我的代码(如下)实际上适用于我使用过的大多数情况:

// public Dictionary<string, object> Cells { get; set; } <-- relevant field from 'SortObject'

List<SortObject> sortObjects = GetSortObjects(); // This is simplified, the data has a few different sources
IOrderedEnumerable<SortObject> orderedList = sortObjects.OrderBy(p => p.Cells["ArbitraryKey"]);
SortObject firstObject = sortedList.First();

// other work with 'orderedList' follows

The problem occurs when I'm trying to OrderBy objects of the DateTime type, and some of those objects are not set and default to 'System.DBNull'.当我尝试使用 DateTime 类型的 OrderBy 对象时会出现问题其中一些对象未设置并且默认为“System.DBNull”。 In this case an exception is thrown when calling 'sortedList.First()' or in any of the later references to 'sortedList'.在这种情况下,调用 'sortedList.First()' 或以后对 'sortedList' 的任何引用都会引发异常。 The exception is simple: " Object must be of type DateTime ", which seems to be a consequence of OrderBy trying to compare the type DateTime to the type DBNull.例外很简单:“ Object 必须是 DateTime 类型”,这似乎是 OrderBy 尝试将 DateTime 类型与 DBNull 类型进行比较的结果。

I've tried two solutions that haven't worked so far:我尝试了两种到目前为止还没有奏效的解决方案:

Attempt One: Set DBNull to new DateTime.尝试一:将 DBNull 设置为新的 DateTime。 In theory this should work, but I would need to create not simply DateTime type objects, but objects of any arbitrary type on the fly.理论上这应该可行,但我不仅需要创建 DateTime 类型的对象,还需要动态创建任意类型的对象。 (I'd also need to take note of these SortObjects and set their data back to DBNull once I had the order correct; I can't be actually changing data after all). (我还需要注意这些 SortObjects 并将它们的数据设置回 DBNull 一旦我的顺序正确;我实际上不能真正更改数据)。

Attempt Two: Organize just DBNull, then just DateTime, then slap them together.尝试二:只组织 DBNull,然后只组织 DateTime,然后将它们打在一起。 Again this might work in theory, but the "other work" mentioned in the code snippet is extensive, including reordering using ThenBy() an arbitrary number of times on any key(s).同样,这在理论上可能有效,但代码片段中提到的“其他工作”是广泛的,包括使用 ThenBy() 对任何键进行任意次数的重新排序。 Doubling its complexity is not an elegant solution and I consider it a backup.将其复杂性加倍并不是一个优雅的解决方案,我认为它是一种备份。

What is the best way to resolve this?解决此问题的最佳方法是什么?

PS: For OrderBy and DateTime I'm using the Microsoft .NET Framework v4.6.2 PS:对于OrderByDateTime ,我使用的是 Microsoft .NET Framework v4.6.2

Change the OrderBy statement to OrderBy(v => v is DBNull? null: v)将 OrderBy 语句更改为OrderBy(v => v is DBNull? null: v)

OrderBy can handle nulls, but not dbNulls. OrderBy 可以处理空值,但不能处理 dbNulls。

That code should work for all the data types该代码应该适用于所有数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM