我有列搜索過濾器值發送到我的網頁api,我無法計算如何使where子句動態?如何使用名稱值集合創建動態where子句?
請看下面(太多代碼)!
名稱值集合:
public DataTablePager<AccountDTO> Get([FromUri] DataTableParameter param)
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
鑄造搜索值:
if (!String.IsNullOrEmpty(nvc["sSearch_0"]) && !int.TryParse(nvc["sSearch_0"], out tmpInt) ||
!String.IsNullOrEmpty(nvc["sSearch_1"]) && !int.TryParse(nvc["sSearch_1"], out tmpInt) ||
!String.IsNullOrEmpty(nvc["sSearch_10"]) && !int.TryParse(nvc["sSearch_10"], out tmpInt)
設置WHERE子句:
filteredresults = filteredresults.Where(i => CorrectNumericTypes
&& (Lead_ID == null || i.Lead_ID == Lead_ID)
&& (Account_ID == null || i.Account_ID == Account_ID)
使用謂詞建設者。關於動態構建謂詞有很多問題(和答案)。 –
謂語是我需要的,謝謝阿諾德。希望我能給你點數! – Chaka