2016-06-10 72 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