0
我不知道是否有人能向我解釋在運行時檢索LINQ表達式身體的ToString
(Expression<Func<T, bool>>) Expression.Body
,並作爲一個字符串在這個操作的缺點?
例如給出以下
public partial class Tests : Form
{
public Tests()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestClass t = new TestClass();
textBox1.Text = t.Evaluate(templates => templates.usable == true && templates.name == "tc1");
}
}
public class TestClass
{
public string name { get; set; }
public bool usable { get; set; }
}
public static class Helpers
{
public static string Evaluate<T>(this T input,Expression<Func<T, bool>> Expression) where T : class
{
return Expression.Parameters.Single().Name + " " + Expression.Body;
}
}
返回
templates ((templates.usable == True) AndAlso (templates.name == "tc1"))
我想知道,可以檢索到該出現什麼樣的性能問題,然後通過正則表達式表達解析它
回答這個問題爲什麼?我一直在玩Dapper了一下,但沒有一個現有的擴展(我見過)吸引我
我想能夠以類似的方式對EF進行操作
_repo.Select(templates=>templates.usable== true && templates.id == templateId);
沒有執行得相當好肯定不能是檢索字符串和操作字符串的缺點。當你試圖將結果*字符串作爲結構化數據處理時,潛在的不利之處就在於此。當你解析它時,你將如何處理你無法用'Expression'本身來做的結果? – AakashM
(templates.usable == True)AndAlso(templates.name ==「tc1」)非常類似於(templates.usable = True)和(templates.name =「tc1」),它可以用作where語句一個選擇語句。在(templates => templates.usable == true && templates.id == templateId)的情況下,它應該是(templates => templates.usable == true && templates.id == id),它與(模板.usable = true AND templates.id = @id) 模板也是要查詢的表的名稱。所以我的想法是「交換」== for =,也是等等 –
我的觀點是,無論您要從其*文本表示*中提取的信息是否已經存在於「Expression」本身中,所以您會更好關閉詢問*,而不是必須解析文本。我沒有看過整個表達式訪問者的東西(在待辦事項列表中!),但是當你爲表達式樹和表達式訪問者的谷歌時,有資源[任意例子](http://www.aboutmycode.com/net -ifwork/building-expression-evaluator-with-expression-trees-in-csharp-part-1 /) – AakashM