我目前正在將舊系統遷移到.Net,並且我遇到了這個問題。 我想返回結果,但我仍然需要在函數後對其進行優化,但使用此代碼,我沒有選擇,只能調用數據庫的結果並過濾內存中的結果,而不是性能差。LINQ到實體的動態查詢
public IQueryable<User> GetUser(string[] accessCodes)
{
string condition = "";
if (accessCodes == null)
{
condition = " AccessCode IS NOT NULL "
}
else
{
for (int i = 0; i <= accessCodes.Length - 1; i++)
{
condition += " AccessCode LIKE '%" + accessCodes[i].ToString() + "%' ";
if (i + 1 <= code.Length - 1)
{
condition += " OR ";
}
}
}
return context.ExecuteQuery<User>("SELECT * FROM User WHERE " + condition, null).ToList();
}
我試過這種方法,但我又卡:
public IQueryable<User> GetUser(string[] accessCodes)
{
IQueryable<User> basequery = from u in context.User
select u;
if (accessCodes == null)
{
basequery = basequery.Where(n => n.AccessCode != null);
}
else
{
for (int i = 0; i <= accessCodes.Length - 1; i++)
{
// what am I supposed to do here?
}
}
return basequery;
}
我希望有不需要第三方庫的解決方案。
你能否簡單地解釋一下你試圖達到的目標?你想根據什麼標準過濾用戶? –