[英]Linq to XMl sorting method
嗨,我有一個datagridview,它手動綁定XML。
我希望通過單擊列標題來對列進行排序。
這是我寫的方法:單擊列標題時,它將獲取列標題並按該列標題對數據進行排序。
我還在其上放置了一個切換開關(方向),因此當用戶再次單擊時,數據可以按不同的順序排序(升/降)
public BindingSource BindXML(string file, string headerName, bool direction)
{
XElement record = XElement.Load(file);
var q = from r in record.Descendants("record")
//ascending order?
orderby (string)r.Element(headerName)
select new
{
work_pack = (int)r.Element("work_pack"),
Locational_Details = (string)r.Element("Locational_Details"),
RegimeName = (string)r.Element("RegimeName")
};
if (direction)
{
//descending order
q.OrderByDescending(r => r);
}
return new BindingSource(q, null);}
問題是lambda表達式q.OrderByDescending(r => r);
根本不起作用
我什至嘗試了q.OrderByDescending(r => r.RegimeName)
和q.OrderByDescending(r => r.Element(headerName));
它們都不起作用。 有什么幫助嗎?
與List<T>.Sort()
, IEnumerable<T>.OrderBy()
不是就地排序。
將您的代碼更改為q = q.OrderByDescending(r => r.HeaderName);
它應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.