從字串動態計算LINQ 表達式
問題:
問題:問題:
給定一個表示LINQ 表達式,例如「(Person.Age > 3 AND Person.Weight > 50) OR Person.Age 可以針對 Person 類別的實例進行評估。
過度烘焙的方法:using System; using System.Linq.Expressions; using System.Linq.Dynamic; namespace ExpressionParser { class Program { public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } static void Main() { // Input expression string const string exp = @"(Person.Age > 3 AND Person.Weight > 50) OR Person.Age 涉及 ANTLR 語法和謂詞產生器框架的建議解決方案可能不必要地複雜。 <p></p><p>替代解決方案:動態LINQ<strong></strong></p>動態 LINQ 函式庫提供了一個優雅且簡單的解決方案。使用方法如下:
多功能性: 支援多種表達式運算子和類型。
輕量級: 只包含解析與計算表達式所需的功能.注意:記得包含System.Linq .Dynamic nuget 套件使此程式碼能夠運作。以上是如何從 C# 中的字串動態計算 LINQ 表達式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!