Answers:
.Where(oh => oh.Hierarchy.Contains("/12/"))
您也可以使用.StartsWith()
或.EndsWith()
。
StartsWith("abc")
被转换成LIKE 'abc%'
与EndsWith("abc")
被cnoverted到LIKE '%abc'
.ToLower().Contains()
如果您想忽略大小写,请不要忘记等。是否要这样做当然取决于您是否尝试从不区分大小写的排序规则中模仿数据库中的LIKE。
用这个:
from c in dc.Organization
where SqlMethods.Like(c.Hierarchy, "%/12/%")
select *;
SqlMethods
使用“点符号”?
System.Data.Linq.SqlClient
名称空间。
我假设您正在使用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 }
注意: * =如果在.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。
@
符号来转义您的字符串,但我意识到这可能只是遵循的一个好习惯。
好吧indexOf也对我有用
var result = from c in SampleList
where c.LongName.IndexOf(SearchQuery) >= 0
select c;
使用这样的代码
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;
}
我总是这样做:
from h in OH
where h.Hierarchy.Contains("/12/")
select h
我知道我不使用like语句,但是在后台将它翻译成带有like语句的查询是可行的。
System.Data.Linq.SqlClient.SqlMethods.Like("mystring", "%string")
对于那些像我这样在寻找如何在LINQ中实现“ SQL Like”方法的人来说,我的工作非常好。
我无法以任何方式更改数据库来更改列排序规则。因此,我必须在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.Locations where 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;
}
}
如果您有任何建议要改进此方法,请您多多包涵。
太晚了,但是我将其结合在一起,以便能够使用SQL Like样式通配符进行字符串比较:
public static class StringLikeExtensions
{
/// <summary>
/// Tests a string to be Like another string containing SQL Like style wildcards
/// </summary>
/// <param name="value">string to be searched</param>
/// <param name="searchString">the search string containing wildcards</param>
/// <returns>value.Like(searchString)</returns>
/// <example>value.Like("a")</example>
/// <example>value.Like("a%")</example>
/// <example>value.Like("%b")</example>
/// <example>value.Like("a%b")</example>
/// <example>value.Like("a%b%c")</example>
/// <remarks>base author -- Ruard van Elburg from StackOverflow, modifications by dvn</remarks>
/// <remarks>converted to a String extension by sja</remarks>
/// <seealso cref="/programming/1040380/wildcard-search-for-linq"/>
public static bool Like(this String value, string searchString)
{
bool result = false;
var likeParts = searchString.Split(new char[] { '%' });
for (int i = 0; i < likeParts.Length; i++)
{
if (likeParts[i] == String.Empty)
{
continue; // "a%"
}
if (i == 0)
{
if (likeParts.Length == 1) // "a"
{
result = value.Equals(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
else // "a%" or "a%b"
{
result = value.StartsWith(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
}
else if (i == likeParts.Length - 1) // "a%b" or "%b"
{
result &= value.EndsWith(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
else // "a%b%c"
{
int current = value.IndexOf(likeParts[i], StringComparison.OrdinalIgnoreCase);
int previous = value.IndexOf(likeParts[i - 1], StringComparison.OrdinalIgnoreCase);
result &= previous < current;
}
}
return result;
}
/// <summary>
/// Tests a string containing SQL Like style wildcards to be ReverseLike another string
/// </summary>
/// <param name="value">search string containing wildcards</param>
/// <param name="compareString">string to be compared</param>
/// <returns>value.ReverseLike(compareString)</returns>
/// <example>value.ReverseLike("a")</example>
/// <example>value.ReverseLike("abc")</example>
/// <example>value.ReverseLike("ab")</example>
/// <example>value.ReverseLike("axb")</example>
/// <example>value.ReverseLike("axbyc")</example>
/// <remarks>reversed logic of Like String extension</remarks>
public static bool ReverseLike(this String value, string compareString)
{
bool result = false;
var likeParts = value.Split(new char[] {'%'});
for (int i = 0; i < likeParts.Length; i++)
{
if (likeParts[i] == String.Empty)
{
continue; // "a%"
}
if (i == 0)
{
if (likeParts.Length == 1) // "a"
{
result = compareString.Equals(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
else // "a%" or "a%b"
{
result = compareString.StartsWith(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
}
else if (i == likeParts.Length - 1) // "a%b" or "%b"
{
result &= compareString.EndsWith(likeParts[i], StringComparison.OrdinalIgnoreCase);
}
else // "a%b%c"
{
int current = compareString.IndexOf(likeParts[i], StringComparison.OrdinalIgnoreCase);
int previous = compareString.IndexOf(likeParts[i - 1], StringComparison.OrdinalIgnoreCase);
result &= previous < current;
}
}
return result;
}
}
5
对like-operator标签具有投票权。我可以请您建议像sql这样的同义词吗?