11
我試圖通過創建一般處理過濾的擴展方法來清理我的代碼。是否有可能使用linq反射到實體?
這是我正在嘗試清理的代碼。
var queryResult = (from r in dc.Retailers select r);
if (!string.IsNullOrEmpty(firstName))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(firstName.Trim(), ex.FirstName.Trim()) > 0);
if (!string.IsNullOrEmpty(lastName))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(lastName.Trim(), ex.LastName.Trim()) > 0);
if (!string.IsNullOrEmpty(companyName))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(companyName.Trim(), ex.CompanyName.Trim()) > 0);
if (!string.IsNullOrEmpty(phone))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(phone.Trim(), ex.Phone.Trim()) > 0);
if (!string.IsNullOrEmpty(email))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(email.Trim(), ex.Email.Trim()) > 0);
if (!string.IsNullOrEmpty(city))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(city.Trim(), ex.City.Trim()) > 0);
if (!string.IsNullOrEmpty(zip))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(zip.Trim(), ex.Zip.Trim()) > 0);
if (!string.IsNullOrEmpty(country))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(country.Trim(), ex.Country.Trim()) > 0);
if (!string.IsNullOrEmpty(state))
queryResult = queryResult.Where(ex => SqlFunctions.PatIndex(state.Trim(), ex.State.Trim()) > 0);
這顯然很重複。所以我嘗試創建一個擴展方法,通過使用反射的屬性進行過濾。這是方法。
public static void FilterByValue<T>(this IQueryable<T> obj, string propertyName, string propertyValue)
{
if (!string.IsNullOrEmpty(propertyValue))
{
obj =
obj.Where(
ex =>
SqlFunctions.PatIndex(propertyValue.Trim(), (string)typeof(T).GetProperty(propertyName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).GetValue(ex)) > 0
);
}
}
,它是被稱爲像這樣:
var queryResult = (from r in dc.Retailers select r);
queryResult.FilterByValue("firstname", firstName);
但是,LINQ的執行時,我得到一個錯誤,指出「的GetValue」是不是在LINQ公認的實體。
那麼,有沒有其他的方式來清理它,還是我不得不離開它醜陋?
是的,這做到了。謝謝。現在我只需再次重新閱讀您的答案來理解它。 – Smeegs
@Smeegs它可以幫助調試一些實際值,並看看創建的表達式(甚至可能有一些中間值我不存儲在變量中)。它有助於將其可視化。表達式通常具有明智的'ToString'實現。 – Servy
你正在使用哪個程序集/名稱空間?我看到SqlFunctions在'EntityFramework.SqlServer'和'System.Data.Entity'中......我假設這些是不同的,對吧? – David