2012-09-01 37 views

回答

2

如果你想檢查表達式是平等的,不只是他們始終評估以同樣的方式,你可以這樣做:

exp1.ToString() == exp2.ToString() 

注意,即使微不足道的變化將導致此返回false,像使它j => j.Contains(firstname)或使用exp2從這個類:

public class Test 
{ 
    static string firstname; 
    public static Expression<Func<string, bool>> exp2 = s => s.Contains(firstname); 
} 

(甚至日ough的lambda表達式看起來相同的代碼中,ToString秀,一個是使用Test.firstname,另一種是使用編譯器生成的類的firstname

不過,這可能會因您的表現來自有用。