C# 类似Linq或其他结构
我的解决方案中有DB oracle。 我想在这个查询中得到一些结果。 查询示例:C# 类似Linq或其他结构,c#,sql,linq,oracle,sql-like,C#,Sql,Linq,Oracle,Sql Like,我的解决方案中有DB oracle。 我想在这个查询中得到一些结果。 查询示例: select * from doctor where doctor.name like '%IVANOV_A%'; 但如果我在林克这样做,我就不会有任何结果 from p in repository.Doctor.Where(x => x.Name.ToLower().Contains(name)) select p; 其中“name”是字符串参数的变量 Web布局请求下一个字符串:“Ivanov a”或
select * from doctor where doctor.name like '%IVANOV_A%';
但如果我在林克这样做,我就不会有任何结果
from p in repository.Doctor.Where(x => x.Name.ToLower().Contains(name))
select p;
其中“name”是字符串参数的变量
Web布局请求下一个字符串:“Ivanov a”或“a Ivanov”
但我建议用户选择您的模式进行查询
如果姓名由“名字”和“姓氏”组成,但用户不知道您的医生的全名,如何获取“患者姓名”
PS:我被迫使用下一个sql:
select *
from doctor
where doctor.name like '%Ivano%' and doctor.name like '%Serg%';
试试这个,它可能会工作(没有测试):
你可以这么做
repository.Doctor.Where(x => x.Name.Contains(name))
在LINQtoSQL和LINQtoEntities中,这都被转换为类似%…%%的。您甚至可能不需要ToLower
,因为比较完全是数据库端的,所以使用数据库的排序规则。您必须尝试一下,但通常数据库在默认情况下是不区分大小写的。但是如果需要,您可以使用ToLower
,它将转换为SQL
至于名称顺序问题。无论得到什么搜索字符串,都可以使用尾随空格和前导空格来查找匹配项。假设搜索字符串是“ab”。匹配项应该是((像“%A%”和“%B%”)或(像“%A%”和“%B%”)。(请输入空格字符!)。可以通过在空格处拆分字符串来解决此问题
[System.Data.Objects.DataClasses.EdmFunction("WebDataModel", "String_Like")]
public static bool Like(this string input, string pattern)
{
/* Turn "off" all regular expression related syntax in
* the pattern string. */
pattern = Regex.Escape(pattern);
/* Replace the SQL LIKE wildcard metacharacters with the
* equivalent regular expression metacharacters. */
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
/* The previous call to Regex.Escape actually turned off
* too many metacharacters, i.e. those which are recognized by
* both the regular expression engine and the SQL LIKE
* statement ([...] and [^...]). Those metacharacters have
* to be manually unescaped here. */
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
}
如果这样更改,会有结果吗<代码>来自repository.Doctor.Where(x=>x.Name.ToLower().Contains(Name.ToLower())中的p)选择p我在想你能猜到。在我的代码“name.ToLower()”中写入。问题不在于。不,伙计们,不。你们真的理解我吗?
[System.Data.Objects.DataClasses.EdmFunction("WebDataModel", "String_Like")]
public static bool Like(this string input, string pattern)
{
/* Turn "off" all regular expression related syntax in
* the pattern string. */
pattern = Regex.Escape(pattern);
/* Replace the SQL LIKE wildcard metacharacters with the
* equivalent regular expression metacharacters. */
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
/* The previous call to Regex.Escape actually turned off
* too many metacharacters, i.e. those which are recognized by
* both the regular expression engine and the SQL LIKE
* statement ([...] and [^...]). Those metacharacters have
* to be manually unescaped here. */
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
}