ユーザーが条件ステートメントをUIから文字列として提供するという要件があります。これは、LINQ to XMLクエリに組み込む必要があります。動的linqクエリについてこのサイトを参照しましたが、LINQ to SQLのみを指定し、LINQ to XMLは指定していません。これが必要です。
https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
これを私の要件に合うように編集してみましたが、うまくいかないようです。
string xml = @"
<Results>
<Result>
<Name>John</Name>
<Phone>110</Phone>
<Location>USA</Location>
</Result>
<Result>
<Name>Mary</Name>
<Phone>120</Phone>
<Location>UK</Location>
</Result>
<Result>
<Name>John</Name>
<Phone>130</Phone>
<Location>Canada</Location>
</Result>
</Results>
";
XElement results = XElement.Parse(xml);
var query = results.Elements("Result")
.AsQueryable().
.OrderBy("Element(XName.Get(\"Name\")).Value
ascending, Element(XName.Get(\"Phone\")).Value ascending");
foreach (var i in query)
{
Console.WriteLine(i);
}
このコードを実行すると、エラーが発生します
System.Linq.Dynamic.ParseException' occurred in System.Linq.Dynamic.dll
Additional information: No property or field XName' exists in type 'XElement'
これを修正する方法はありますか?
XElement results = XElement.Parse(xml);
var query = results.Elements("Result").AsQueryable().OrderBy(x=>x.Element("Name").Value).ThenBy(x=>x.Element("Phone").Value);
流暢な構文とクエリ構文は異なるものです。混同しないでください。