简体   繁体   English

Linq中的类似运算符(用于整数)

[英]Like operator in Linq(For integer)

In Linq we use the like operator for strings 在Linq中,我们对字符串使用like运算符

var query = from c in ctx.Customers
            where c.City.StartsWith("L") && c.City.Contains("n")
            select c

Is it Possible to apply like operator in linq for integers. 是否有可能在linq中将运算符应用于整数。

please help me. 请帮我。

No. You'd have to cast to string first (see the answer here also: Problem with converting int to string in Linq to entities ). 否。您必须首先强制转换为字符串(另请参见此处的答案: 将Linq中的int转换为string转换为Entity时出现问题 )。

In SQL it would be something like 在SQL中会像

DECLARE @StartsWith NVARCHAR
SET @StartsWith = '1'

SELECT * FROM Customers
WHERE (CAST CustomerId AS NVARCHAR(MAX)) LIKE @StartsWith + '%'

I guess this should work in LINQ: 我猜这应该在LINQ中工作:

var query = from c in ctx.Customers
            where SqlFunctions.StringConvert((Decimal)c.CustomerId).Startswith("1")
            select c;

note that this will be a slow query if you have a large table, as it will need to do a table scan (any index on the int column would not be used). 请注意,如果您的表很大,这将是一个缓慢的查询,因为它需要进行表扫描(不会使用int列上的任何索引)。

.Contains(“ / Integer /”))也可以使用.StartsWith()或.EndsWith()。

You can convert your integer field to a string, and then use SqlMethods.Like : 您可以将整数字段转换为字符串,然后使用SqlMethods.Like

var query = from c in ctx.Customers
            where SqlMethods.Like(c.IntegerPhoneNumber.ToString(), "555*")
            select c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM