如何排序通过使用缺少元素的Linq

本文关键字:元素 Linq 何排序 排序 | 更新日期: 2023-09-27 18:27:57

通常在使用Linq时,我通常会使用Where或类似功能过滤掉空/空记录。但是,我需要根据多个条件订购列表,并按顺序保留列表中的所有项目。

以下仅适用于列表中所有项目的.Dimension1.Count > 0

var orderedList = mList.Elements
                       .OrderBy(x => x.Property1)
                       .ThenBy(x => x.Property2)
                       .ThenBy(x => x.Property3.Dimension1[0].Value2)
                       .ToList();

如果任何元素都有Dimension1.Count == 0,那么我会得到错误:

'索引超出范围。必须是非负数并且小于集合的大小。'

这是预期的,因为数组没有标注尺寸。

当列表中包含具有.Dimension1.Count = 0的项目时,有什么方法可以实现这一点吗?

请注意,Dimension1[0].Value2是类型double。

如何排序通过使用缺少元素的Linq

您可以这样做:

var orderedList = mList.Elements.OrderBy(x => x.Property1)
.ThenBy(x => x.Property2)
.ThenBy(x => x.Property3.Dimension1.Count == 0
    ? -1
    : x.Property3.Dimension1[0].Value2)
.ToList();

这里我假设Value2是一个整数。例如,如果是字符串,则可以使用null而不是-1

其思想是当Count为0时使用一个特殊值。

例如,您没有提及是否使用实体框架。这是否会转换为有效的SQL语句是值得怀疑的(它可能..),但在您的情况下,这可能值得一试。

.ThenBy(x => x.Property3.Dimension1[0].Count > 0 ? x.Property3.Dimension1[0].Value2 : -1)

我假设Value2总是> 0,所以任何缺少值的记录都应该被分配给-1,并在列表中进一步向下推。如果不是这样,您可能需要将-1更改为更适合您的情况的内容。

您只需为第二个ThenBy提供一个默认值。

.ThenBy(x => x.Property3.Dimension1.Any() 
             ? x.Property3.Dimension1[0].Value2 
             : // Some default value for the type of Value2.
               // Go high or low depending on the type to put 
               // them at the top or bottom of the list
        );

如果您想确保Dimensions1数组不会为null异常:

.Then(x => (x.Property3.Dimensions1 ?? new object[0]).Count > 0 ? x.Property3.Dimensions1[0].Value2 : -1)

有一个专用的LINQ方法DefaultIfEmpty

返回指定序列或指定值的元素如果序列为空,则在singleton集合中。

var orderedList = mList.Elements
                       .OrderBy(x => x.Property1)
                       .ThenBy(x => x.Property2)
                       .ThenBy(x => x.Property3.Dimension1.DefaultIfEmpty(someDefaultValue).ElementAt(0).Value2)
                       .ToList();