简体   繁体   English

LINQ Like子句-不带通配符

[英]LINQ Like Clause - Without Wildcard operator

I would like to write a LINQ to SQL query with a Like operator, but without the WildCard operator on it. 我想使用Like运算符编写LINQ to SQL查询,但不使用WildCard运算符。

I know the following: 我知道以下几点:

Contains will be equivelent to this: a like '%b%' StartsWith will be equivlent to this: a like 'b%' and EndsWith will be vice versa. 包含将与此等价:类似'%b%'的StartsWith将与此等价:一个类似'b%'和EndsWith将相反。

Equals could do it, but "This method performs an ordinal (case-sensitive and culture-insensitive) comparison." 等于可以做到,但是“此方法执行顺序(区分大小写和对文化不敏感)的比较。”

The way I am doing now is like this: 我现在的工作方式是这样的:

var  loc  = (from l in data.Locations.OrderBy(l => l.LocationName)
    join s in data.LocationSecurities.Where(s=> s.UserName.ToLower().Equals(userName.ToLower())) on l.LocationID equals s.LocationId
    select new
    {
        LocationId = l.LocationID,
        Name = l.LocationName
    }
    ).Distinct().ToList();

But this is only a work-around, because what I want to do is to simply use an equivelent of Like but whitout the WildCard, which get visibly clear on the SQL Server Profiler. 但这只是一个变通方法,因为我想做的就是简单地使用“ Like”的等价物,但忽略通配符,这在SQL Server Profiler上显而易见。

Please let me know. 请告诉我。

Thank you in advance. 先感谢您。

You can use: 您可以使用:

SqlMethods.Like

Like: 喜欢:

.Where(s => SqlMethods.Like(s.UserName, userName))

That Equals performs an "ordinal (case-sensitive and culture-insensitive) comparison" does not apply when used with Linq to SQL or Entity framework since the statement will be translated into SQL. 当与Linq to SQL或Entity Framework一起使用时, Equals执行“常规(区分大小写和对文化不敏感)比较”的情况不适用,因为该语句将转换为SQL。
The database collation (or column collation if set) will determine how "=" (Equals) is handled. 数据库排序规则 (或设置为列排序规则)将确定如何处理"=" (等于)。

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

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