简体   繁体   English

C#LINQ orderby

[英]C# LINQ orderby

I have an Xelement containing a number of elements. 我有一个包含许多元素的Xelement。

I have the following code to sort them: 我有以下代码来对它们进行排序:

var calculation = from y in x.Elements("row")
                 orderby y.Element("BUILD_ORDER").Value
                 select new
                 {
                     calcAttribute = y.Element("ELEMENT").Value

                 };

Which works fine, until BUILD_ORDER > 10, it orders 10 just after 1. 哪个工作正常,直到BUILD_ORDER> 10,它在1之后命令10。

If I want it in strict numerical order, I case the element to an Int, is this the correct way to do it, or does LINQ have an inbuilt extension/method? 如果我想按严格的数字顺序,我将元素称为Int,这是正确的方法吗,还是LINQ有一个内置的扩展/方法?

orderby Convert.ToInt32(y.Element("BUILD_ORDER").Value)

LINQ to Objects doesn't have the built in conversion, but LINQ to XML does: LINQ to Objects没有内置转换,但LINQ to XML可以:

var calculation = from y in x.Elements("row")
                  orderby (int) y.Element("BUILD_ORDER")
                  select new
                  {
                      calcAttribute = y.Element("ELEMENT").Value
                  };

Is there any reason why you're using an anonymous type though, rather than just selecting the value you want? 您有没有理由使用匿名类型,而不是仅仅选择您想要的值? For example: 例如:

var calculation = from y in x.Elements("row")
                  orderby (int) y.Element("BUILD_ORDER")
                  select y.Element("ELEMENT").Value;

Note that both of these will throw an exception if the BUILD_ORDER or ELEMENT subelements are missing. 请注意,如果缺少BUILD_ORDER或ELEMENT子元素,则这两个都将引发异常。 You can fix that using a conversion to int? 您可以使用转换为int?来解决这个问题int? instead of int for BUILD_ORDER , and a conversion to string for ELEMENT: 而不是BUILD_ORDERint ,以及ELEMENT的string转换:

var calculation = from y in x.Elements("row")
                  orderby (int?) y.Element("BUILD_ORDER")
                  select (string) y.Element("ELEMENT");

This will still fail if BUILD_ORDER exists but can't be parsed as an integer of course. 如果存在BUILD_ORDER,则仍然会失败,但当然不能将其解析为整数。

If your data should always have these elements, the first form is better, as it'll detect any data errors earlier. 如果您的数据应始终具有这些元素,则第一种形式更好,因为它会更早地检测到任何数据错误。

Looks good to me. 对我来说看上去很好。 You could also use Int32.Parse(...) . 您也可以使用Int32.Parse(...) I'm not aware of any built-in method for sorting strings as integers, but you certainly could write your own. 我不知道任何内置的方法将字符串排序为整数,但你当然可以自己编写。

Yes, since your XML values are not typed, you have to cast them manually for .NET to be able to sort numerically. 是的,因为您的XML值没有输入,所以必须手动为.NET编译它们才能进行数字排序。

By the way, int.Parse will give you slightly better performance. 顺便说一句, int.Parse会给你稍微好一点的性能。

Looks like it's reading the build order as a string instead of an int. 看起来它正在将构建顺序读作字符串而不是int。 Your sample should work, or try 您的样本应该有效,或者尝试

orderby int.Parse(y.Element("BUILD_ORDER").Value)

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

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