像linq c#中的运算符一样用于整数字段 [英] like operator in linq c# for integer field

查看:57
本文介绍了像linq c#中的运算符一样用于整数字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要interger字段的运算符,然后我使用此代码



var details = db.RecipientDescriptions.Where(o => o.ID。 ToString()。包含(67))。ToList();



但我收到错误



LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为商店表达式。



any请问?

解决方案

因为LINQ to Entities查询的表达式是在DB上运行的,所以你已经限制了你可以传递的内容。由于 ToString 只是一种可以做任何事情的方法,因此DB无法保证结果。



如果未过滤的结果集不是太大,你可以做的是在表达式运行后应用 Where 子句;

 db.RecipientDescriptions.ToList()。其中​​(o => o.ID.ToString()。包含(  67)。ToList(); 





更一般的说明我会说如果你需要对一个值 contains 然后该值不应该是整数类型。



希望这有帮助,

Fredrik


复制自:


LINQ to Entities无法识别方法'System.String ToString()'方法 [ ^ ]



使用可以使用这样的东西,



 其中 userIds.Contains(SqlFunctions.StringConvert(( double )user.Id))
而不是其中 userIds.Contains(user.Id.ToString())






实际上你正在使用LINQ to Sql所以这些对象是IQueryable所以没有ToString()方法。



你可以试试这个:



var details = db.RecipientDescriptions.Where(o => SqlMethods.Like(o.ID,67%))。ToList() ;

i want like operator for interger field,then i am using this code

var details = db.RecipientDescriptions.Where(o =>o.ID.ToString().Contains("67")).ToList();

but i am getting the error

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

any solutions please?

解决方案

As the expression past to a LINQ to Entities query is run on the DB you've restricted in what you can pass. Since ToString is just a method that can do anything there's no way for the DB to guarantee the result.

What you can do if your unfiltered result set isn't too large is to apply the Where clause after the expression has been run;

db.RecipientDescriptions.ToList().Where(o =>o.ID.ToString().Contains("67").ToList();



On a more general note I would say that if you have a need to do contains on a value then that value shouldn't be of type integer.

Hope this helps,
Fredrik


Copied from:

LINQ to Entities does not recognize the method 'System.String ToString()' method[^]

use can use something like this,

where userIds.Contains(SqlFunctions.StringConvert((double)user.Id))
instead of where userIds.Contains(user.Id.ToString())


Hi,

Actually you are using LINQ to Sql so those objects are IQueryable so there is no ToString() method.

you can try this :

var details = db.RecipientDescriptions.Where(o =>SqlMethods.Like(o.ID, "67%")).ToList();


这篇关于像linq c#中的运算符一样用于整数字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆