我有一些JSON需要在C#中排序。我試圖使用System.Linq.Dynamic
對JObject
s列表進行排序,但我無法使其與Newtonsoft.Json.Linq.JObject
的實例一起使用。使用動態LINQ對一個或多個屬性排序JSON
這工作:
public class Row
{
public String Column1 { get; set; }
public Int32 Column2 { get; set; }
}
var rows = new List<Row>();
rows.Add(new Row { Column1 = "B", Column2 = 2 });
rows.Add(new Row { Column1 = "B", Column2 = 1 });
rows.Add(new Row { Column1 = "A", Column2 = 2 });
rows.Add(new Row { Column1 = "A", Column2 = 1 });
var sortedRows = rows.AsQueryable().OrderBy("Column1, Column2");
這不:
var rows = new List<JObject>();
var row1 = new JObject();
row1["Column1"] = "B";
row1["Column2"] = 2;
rows.Add(row1);
var row2 = new JObject();
row2["Column1"] = "B";
row2["Column2"] = 1;
rows.Add(row2);
var row3 = new JObject();
row3["Column1"] = "A";
row3["Column2"] = 2;
rows.Add(row3);
var row4 = new JObject();
row4["Column1"] = "A";
row4["Column2"] = 1;
rows.Add(row4);
var sortedRows = rows.AsQueryable().OrderBy("Column1, Column2");
//System.Linq.Dynamic.ParseException : No property or field 'Column1' exists in type 'JObject'
我怎麼能對屬性的動態數量JObject
情況進行排序?
這是完美的!使我免於實施可怕的黑客攻擊。 – tponthieux 2014-10-03 00:20:17