简体   繁体   English

如何使用缺少元素的Linq进行排序?

[英]How to OrderBy using Linq with missing elements?

Usually when using Linq I would usually filter out empty/null records using Where or similar. 通常在使用Linq时,我通常会使用Where或类似过滤掉空/空记录。 However, I need to order a List on multiple criteria and retain all items in the list but in order. 但是,我需要在多个条件上订购一个列表,并按顺序保留列表中的所有项目。

The following works only when .Dimension1.Count > 0 for all items in the list 以下内容仅适用于列表中所有项目的.Dimension1.Count > 0

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

If any of the elements have Dimension1.Count == 0 then I get error: 如果任何元素有Dimension1.Count == 0那么我得到错误:

'Index was out of range. '指数超出范围。 Must be non-negative and less than the size of the collection.' 必须是非负数且小于集合的大小。

Which is expected as the array is not dimensioned. 这是预期的,因为数组没有标注尺寸。

Is there a way to make this work when the list contains items which have .Dimension1.Count = 0 ? 当列表包含.Dimension1.Count = 0项目时,有没有办法使这个工作?

Note that Dimension1[0].Value2 is type double. 请注意, Dimension1[0].Value2是double类型。

You can do something like this: 你可以这样做:

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();

I am assuming here that Value2 is an integer. 我在这里假设Value2是一个整数。 If it is a string for example, you can use null instead of -1 . 例如,如果它是一个字符串,则可以使用null而不是-1

The idea is that you use a special value when Count is 0. 想法是当Count为0时使用特殊值。

You don't mention whether you're using, for example, Entity Framework. 您没有提到您是否正在使用,例如,Entity Framework。 It's doubtful this would convert to a valid SQL statement (it might..), but this may be worth a try in your case. 令人怀疑的是,它会转换为有效的SQL语句(可能......),但在您的情况下这可能值得一试。

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

I'm assuming Value2 is always > 0 , so that any records missing values should be assigned -1 and be pushed further down the list. 我假设Value2始终> 0 ,因此任何记录缺失值都应分配-1并在列表中进一步向下推。 If that's not the case, you may need to change -1 to something more appropriate to your situation. 如果情况并非如此,您可能需要将-1更改为更适合您情况的内容。

You can just provide a default value for the second ThenBy . 您只需为第二个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
        );

There is a dedicated LINQ method for that, DefaultIfEmpty , which 有一个专用的LINQ方法, DefaultIfEmpty ,它

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty. 如果序列为空,则返回指定序列的元素或单例集合中的指定值。

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

如果你想确定Dimensions1数组不会是null异常:

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

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

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