2016-06-10 74 views
0

我正在使用此代碼作爲一個DataGridView過濾器:System.NotSupportedException而試圖通過ID C#過濾

private void consultarPorCriterio() 
    { 

     var inspec = from ins in entities.Inspeccions 
         where (ins.Ralladuras.StartsWith(txtTextoABuscar.Text) || 
           ins.Repuesta.StartsWith(txtTextoABuscar.Text)|| 
           ins.ID.ToString().StartsWith(txtTextoABuscar.Text) 
          ) 
         select ins; 
     dgvInspeccion.DataSource = inspec.ToList(); 
    } 

它過濾如果我刪除:

ins.ID.ToString().StartsWith(txtTextoABuscar.Text).

如果我不」 t刪除那部分代碼我得到這個錯誤:

Exception not handled... 'System.NotSupportedException' in mscorlib.dll

Aditional Info: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

有人知道我在做什麼錯嗎?

+0

,因爲它不能被轉換爲T-SQL,LINQ到實體無法識別它,看看這個答案:http://stackoverflow.com/a/34061692/2946329 –

+0

我從我的老師程序採取此代碼,在他的程序中,他做的和我試圖做的一樣,我的意思是按ID過濾。爲什麼它適合他而不適合我? –

+0

ins.ID的底層數據類型是什麼? – tolanj

回答

0

您可以使用SqlFunctions.StringConvert爲INT轉換爲字符串,稍加修改你的代碼最終可能是這樣的:

SqlFunctions.StringConvert((double)ins.ID).StartsWith(txtTextoABuscar.Text) 
+0

我試過了,它過濾了,但datagridview什麼都沒顯示。 –

+0

你確定你的新查詢正在返回數據嗎?因爲你綁定datagridview的部分似乎沒問題。 – krish