.net 如何在 Linq 中执行 SQL Like %?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/835790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:39:03  来源:igfitidea点击:

How to do SQL Like % in Linq?

.netlinqtsqllinq-to-entitiessql-like

提问by Matt Dell

I have a procedure in SQL that I am trying to turn into Linq:

我在 SQL 中有一个过程,我试图将其转换为 Linq:

SELECT O.Id, O.Name as Organization
FROM Organizations O
JOIN OrganizationsHierarchy OH ON O.Id=OH.OrganizationsId
where OH.Hierarchy like '%/12/%'

The line I am most concerned with is:

我最关心的一行是:

where OH.Hierarchy like '%/12/%'

I have a column that stores the hierarchy like /1/3/12/ for example so I just use %/12/% to search for it.

例如,我有一列存储像 /1/3/12/ 这样的层次结构,所以我只使用 %/12/% 来搜索它。

My question is, what is the Linq or .NET equivalent to using the percent sign?

我的问题是,Linq 或 .NET 相当于使用百分号是什么?

回答by andleer

.Where(oh => oh.Hierarchy.Contains("/12/"))

You can also use .StartsWith()or .EndsWith().

您也可以使用.StartsWith().EndsWith()

回答by L P

Use this:

用这个:

from c in dc.Organization
where SqlMethods.Like(c.Hierarchy, "%/12/%")
select *;

回答by KristoferA

I'm assuming you're using Linq-to-SQL* (see note below). If so, use string.Contains, string.StartsWith, and string.EndsWith to generate SQL that use the SQL LIKE operator.

我假设您使用的是 Linq-to-SQL*(请参阅下面的注释)。如果是,请使用 string.Contains、string.StartsWith 和 string.EndsWith 生成使用 SQL LIKE 运算符的 SQL。

from o in dc.Organization
join oh in dc.OrganizationsHierarchy on o.Id equals oh.OrganizationsId
where oh.Hierarchy.Contains(@"/12/")
select new { o.Id, o.Name }

or

或者

from o in dc.Organization
where o.OrganizationsHierarchy.Hierarchy.Contains(@"/12/")
select new { o.Id, o.Name }

Note:* = if you are using the ADO.Net Entity Framework (EF / L2E) in .net 3.5, be aware that it will not do the same translation as Linq-to-SQL. Although L2S does a proper translation, L2E v1 (3.5) will translate into a t-sql expression that will force a full table scan on the table you're querying unless there is another better discriminator in your where clause or join filters.
Update:This is fixed in EF/L2E v4 (.net 4.0), so it will generate a SQL LIKE just like L2S does.

注意:* = 如果您在 .net 3.5 中使用 ADO.Net 实体框架 (EF / L2E),请注意它不会执行与 Linq-to-SQL 相同的转换。尽管 L2S 进行了适当的转换,但 L2E v1 (3.5) 将转换为 t-sql 表达式,该表达式将强制对您正在查询的表进行全表扫描,除非您的 where 子句或连接过滤器中有另一个更好的鉴别器。
更新:这在 EF/L2E v4 (.net 4.0) 中已修复,因此它会像 L2S 一样生成 SQL LIKE。

回答by robertz

If you are using VB.NET, then the answer would be "*". Here is what your where clause would look like...

如果您使用的是 VB.NET,那么答案将是“*”。这是您的 where 子句的样子...

Where OH.Hierarchy Like '*/12/*'

Note: "*" Matches zero or more characters. Here is the msdn article for the Like operator.

注意:“*”匹配零个或多个字符。这是 Like 运算符的 msdn 文章

回答by Rumplin

Well indexOf works for me too

好吧 indexOf 也适用于我

var result = from c in SampleList
where c.LongName.IndexOf(SearchQuery) >= 0
select c;

回答by Ernesto

Use such code

使用这样的代码

try
{
    using (DatosDataContext dtc = new DatosDataContext())
    {
        var query = from pe in dtc.Personal_Hgo
                    where SqlMethods.Like(pe.nombre, "%" + txtNombre.Text + "%")
                    select new
                    {
                        pe.numero
                        ,
                        pe.nombre
                    };
        dgvDatos.DataSource = query.ToList();
    }
}
catch (Exception ex)
{
    string mensaje = ex.Message;
}

回答by kofifus

.NET core now has EF.Functions.Like

.NET 核心现在有 EF.Functions.Like

回答by ComeIn

In case you are not matching numeric strings, always good to have common case:

如果您不匹配数字字符串,使用常见情况总是好的:

.Where(oh => oh.Hierarchy.ToUpper().Contains(mySearchString.ToUpper()))

回答by H. Pauwelyn

I do always this:

我总是这样做:

from h in OH
where h.Hierarchy.Contains("/12/")
select h

I know I don't use the like statement but it's work fine in the background is this translated into a query with a like statement.

我知道我不使用 like 语句,但它在后台工作正常,将其转换为带有 like 语句的查询。

回答by isuruAb

Try this, this works fine for me

试试这个,这对我来说很好用

from record in context.Organization where record.Hierarchy.Contains(12) select record;