如何通过属性值进行排序在LINQ C#XML?此外MVC [英] How to sort XML in LINQ C# by an attribute value? Also MVC

查看:169
本文介绍了如何通过属性值进行排序在LINQ C#XML?此外MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用最新的MVC 2 RC 2)我试图通过元素的属性值进行排序在LINQ(C#)一些XML ...

(Using the latest MVC 2 RC 2) I'm trying to sort some XML in LINQ (C#) by an element's attribute's value...

var sites = from s in xDoc.Element("sites").Elements("site")
orderby s.Attribute("name")
select s;

但是,当我把它传递给我查看我得到异常:

But when I pass this to my View I get the exception:

Exception Details: System.ArgumentException: At least one object must implement IComparable.
Source Error: 
Line 37:                </th>
Line 38:            </tr>
Line 39:            <% foreach (var item in Model)
Line 40:               { %>
Line 41:            <tr>

有人能告诉我如何使用LINQ to XML进行排序,并让它正确地呈现?

Can someone tell me how to sort XML using LINQ and have it render properly?

推荐答案

编辑:好吧,我觉得你只是想:

Okay, I think you just want:

var sites = from s in xDoc.Element("sites").Elements("site")
            orderby (string) s.Attribute("name")
            select s;

其中也可以写为:

Which could also be written as:

var sites = xDoc.Element("sites")
                .Elements("site")
                .OrderBy(s => (string) s.Attribute("name"));

这篇关于如何通过属性值进行排序在LINQ C#XML?此外MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆