我在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中使用百分号的等价是什么?


当前回答

对于那些像我一样在LINQ中寻找“类似SQL”方法的人来说,我有一些工作得非常好的东西。

我在一个情况下,我不能改变数据库以任何方式改变列排序规则。 所以我必须在LINQ中找到一种方法来做到这一点。

我使用的是辅助方法SqlFunctions。PatIndex的行为类似于真正的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 = (from loc in _context.)位置 SqlFunctions的地方。PatIndex(city, loc.City) > 0 选择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;
        }
    }

如果你们有任何建议来改进这个方法,我很乐意倾听。

其他回答

用这个:

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

Contains在Linq中使用,就像like在SQL中使用一样。

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});
.Where(oh => oh.Hierarchy.Contains("/12/"))

你也可以使用. startswith()或. endswith()。

对于那些像我一样在LINQ中寻找“类似SQL”方法的人来说,我有一些工作得非常好的东西。

我在一个情况下,我不能改变数据库以任何方式改变列排序规则。 所以我必须在LINQ中找到一种方法来做到这一点。

我使用的是辅助方法SqlFunctions。PatIndex的行为类似于真正的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 = (from loc in _context.)位置 SqlFunctions的地方。PatIndex(city, loc.City) > 0 选择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;
        }
    }

如果你们有任何建议来改进这个方法,我很乐意倾听。

indexOf也适用于我

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