首页 文章

如何在Linq中使用SQL Like%?

提问于
浏览
357

我在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/%'

我最关心的是:

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

我有一个存储层的列,例如/ 1/3/12,所以我只使用%/ 12 /%来搜索它 .

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

13 回答

  • 1

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

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

    我总是这样做:

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

    我知道我不使用like语句,但它在后台工作正常,这被翻译成一个类似语句的查询 .

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

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

  • 0

    用这个:

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

    我假设您正在使用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 }
    

    要么

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

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

  • 4

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

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

    注意:"*"匹配零个或多个字符 . Here is the msdn article for the Like operator .

  • 506

    那么indexOf也适合我

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

    使用这样的代码

    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;
    }
    
  • 1

    如果你不匹配数字字符串,总是有一个常见的情况:

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

    包含在Linq中使用,就像在SQL中使用Like一样 .

    string _search="/12/";
    

    . . .

    .Where(s => s.Hierarchy.Contains(_search))
    

    您可以在Linq中编写SQL脚本,如下所示:

    var result= Organizations.Join(OrganizationsHierarchy.Where(s=>s.Hierarchy.Contains("/12/")),s=>s.Id,s=>s.OrganizationsId,(org,orgH)=>new {org,orgH});
    
  • 0

    对于那些像我一样在LINQ中寻找“SQL Like”方法的方式,我有一些非常好的东西 .

    我遇到的情况是我无法以任何方式更改数据库以更改列排序规则 . 所以我要在LINQ中找到一种方法来做到这一点 .

    我正在使用辅助方法 SqlFunctions.PatIndex witch act与真正的SQL LIKE运算符类似 .

    首先,我需要在搜索值中枚举所有可能的变音符号(我刚刚学到的一个词),以获得类似于:

    déjà     => d[éèêëeÉÈÊËE]j[aàâäAÀÂÄ]
    montreal => montr[éèêëeÉÈÊËE][aàâäAÀÂÄ]l
    montréal => montr[éèêëeÉÈÊËE][aàâäAÀÂÄ]l
    

    然后在LINQ中例如:

    var city =“montr [éèêëeÉÈÊËE] [aàâäAÀÂÄ] l”;
    var data =(来自_context.Locations中的loc
    其中SqlFunctions.PatIndex(city,loc.City)> 0
    select loc.City) . ToList();

    所以根据我的需要,我编写了一个Helper / Extension方法

    public static class SqlServerHelper
        {
    
            private static readonly List<KeyValuePair<string, string>> Diacritics = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("A", "aàâäAÀÂÄ"),
                new KeyValuePair<string, string>("E", "éèêëeÉÈÊËE"),
                new KeyValuePair<string, string>("U", "uûüùUÛÜÙ"),
                new KeyValuePair<string, string>("C", "cçCÇ"),
                new KeyValuePair<string, string>("I", "iîïIÎÏ"),
                new KeyValuePair<string, string>("O", "ôöÔÖ"),
                new KeyValuePair<string, string>("Y", "YŸÝýyÿ")
            };
    
            public static string EnumarateDiacritics(this string stringToDiatritics)
            {
                if (string.IsNullOrEmpty(stringToDiatritics.Trim()))
                    return stringToDiatritics;
    
                var diacriticChecked = string.Empty;
    
                foreach (var c in stringToDiatritics.ToCharArray())
                {
                    var diac = Diacritics.FirstOrDefault(o => o.Value.ToCharArray().Contains(c));
                    if (string.IsNullOrEmpty(diac.Key))
                        continue;
    
                    //Prevent from doing same letter/Diacritic more than one time
                    if (diacriticChecked.Contains(diac.Key))
                        continue;
    
                    diacriticChecked += diac.Key;
    
                    stringToDiatritics = stringToDiatritics.Replace(c.ToString(), "[" + diac.Value + "]");
                }
    
                stringToDiatritics = "%" + stringToDiatritics + "%";
                return stringToDiatritics;
            }
        }
    

    如果你们有任何建议要加强这种方法,我会很高兴听到你的意见 .

  • 2

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

  • 27
    System.Data.Linq.SqlClient.SqlMethods.Like("mystring", "%string")
    

相关问题