C# 我如何动态构建一个linq或查询,其中几个列中的一个可以匹配搜索字符串?
我如何动态构建一个linqC# 我如何动态构建一个linq或查询,其中几个列中的一个可以匹配搜索字符串?,c#,sql,linq,C#,Sql,Linq,我如何动态构建一个linq或查询,其中几个列中的一个可以匹配搜索字符串?这在SQL中并不难做到。例如: SELECT ID, Title, Description, Comments, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn FROM Category WHERE (Title LIKE '%Supplier%') OR (Description LIKE '%Supplier%') 有时我需要添加或(注
或查询,其中几个列中的一个可以匹配搜索字符串?这在SQL中并不难做到。例如:
SELECT ID, Title, Description, Comments, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn
FROM Category
WHERE (Title LIKE '%Supplier%')
OR (Description LIKE '%Supplier%')
有时我需要添加或(注释,如“%Supplier%”)
或省略或(标题,如“%Supplier%”)
,这就是我想要动态构建它的原因
我不认为这是一个重复的问题,因为潜在的重复问题不会以使用Stackoverflow搜索功能可以找到的方式被询问。您只需像在C#中的其他地方一样使用or运算符即可。下面的代码片段基本上应该是您想要的。字符串文字可以改为局部变量
MyObjects.Where(x => x.Someprop == "my string" || x.SomeOtherprop == "my string").Select(...)
以你的例子来说,你实际上想要更像
Where(x => x.Comments.Contains("Supplier") || x.Description.Contains("Supplier"))
注意,这些SQL通配符等价于C#方法将是:;包含、开始和结束。您还可以使用正则表达式来匹配更复杂的模式。您只需像在C#中的其他地方一样使用or运算符即可。下面的代码片段基本上应该是您想要的。字符串文字可以改为局部变量
MyObjects.Where(x => x.Someprop == "my string" || x.SomeOtherprop == "my string").Select(...)
以你的例子来说,你实际上想要更像
Where(x => x.Comments.Contains("Supplier") || x.Description.Contains("Supplier"))
注意,这些SQL通配符等价于C#方法将是:;包含、开始和结束。您还可以使用正则表达式来匹配更复杂的模式。以下是LINQ示例:
public IEnumerable<Items> FindBySupplier(string supplier)
{
return context.Items.Where(i => i.Title.ToLower().Contains(supplier.ToLower()) || i.Description.ToLower().Contains(supplier.ToLower());
public IEnumerable FindBySupplier(字符串供应商)
{
返回context.Items.Where(i=>i.Title.ToLower().Contains(supplier.ToLower())| | i.Description.ToLower().Contains(supplier.ToLower());
}以下是LINQ示例:
public IEnumerable<Items> FindBySupplier(string supplier)
{
return context.Items.Where(i => i.Title.ToLower().Contains(supplier.ToLower()) || i.Description.ToLower().Contains(supplier.ToLower());
public IEnumerable FindBySupplier(字符串供应商)
{
返回context.Items.Where(i=>i.Title.ToLower().Contains(supplier.ToLower())| | i.Description.ToLower().Contains(supplier.ToLower());
}使用Lambda。可能适合您
context.Category.Where(x=>x.Title.Contains("Supplier") || x.Description.Contains("Supplier")).Select(x=>new {....});
使用Lambda。可能对你有用
context.Category.Where(x=>x.Title.Contains("Supplier") || x.Description.Contains("Supplier")).Select(x=>new {....});
使用或
动态生成查询不像使用和
生成查询那样简单。如果设置为动态生成查询,则可以使用。如果您不介意使用可以动态忽略其部分条件的单个查询,则可以使用以下查询:
bool checkTitle = ...
bool checkDescription = ...
string likeString = ...
var res = context.Category
.Where(item =>
(checkTitle && item.Title.Contains(likeString))
||
(checkDescription && item.Description.Contains(likeString))
);
由于str.Contains(“xyz”)
转换为类似于“%xyz%”的str
,因此此查询将对应于如下SQL查询:
SELECT ID, Title, Description, Comments, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn
FROM Category
WHERE (@checkTitle=1 AND Title LIKE '%Supplier%')
OR (@checkDescription=1 AND Description LIKE '%Supplier%')
使用或
动态生成查询不像使用和
生成查询那样简单。如果设置为动态生成查询,则可以使用。如果您不介意使用可以动态忽略其部分条件的单个查询,则可以使用以下查询:
bool checkTitle = ...
bool checkDescription = ...
string likeString = ...
var res = context.Category
.Where(item =>
(checkTitle && item.Title.Contains(likeString))
||
(checkDescription && item.Description.Contains(likeString))
);
由于str.Contains(“xyz”)
转换为类似于“%xyz%”的str
,因此此查询将对应于如下SQL查询:
SELECT ID, Title, Description, Comments, CreatedBy, CreatedOn, UpdatedBy, UpdatedOn
FROM Category
WHERE (@checkTitle=1 AND Title LIKE '%Supplier%')
OR (@checkDescription=1 AND Description LIKE '%Supplier%')
最后,我结合evanmcdonnal和dasblinkenlight的想法解决了这个问题。我给了他们每人一张赞成票,因为不幸的是,我不能给他们每个帮助回答这个问题的积分。这就是我最后写的
private static IQueryable<Db.Category> FilterContains(string searchFor, string excludedBrFields, IQueryable<Db.Category> categories)
{
if (searchFor.StartsWith("*") && searchFor.EndsWith("*"))
{
searchFor = searchFor.Substring(1, searchFor.Length - 2);
}
searchFor = searchFor.ToLower();
var predicate = PredicateBuilder.False<Db.Category>();
if (!excludedBrFields.Contains("Title"))
{
predicate = predicate.Or(x => x.Title.ToLower().Contains(searchFor));
}
if (!excludedBrFields.Contains("Description"))
{
predicate = predicate.Or(x => x.Description != null && x.Description.ToLower().Contains(searchFor));
}
if (!excludedBrFields.Contains("Comments"))
{
predicate = predicate.Or(x => x.Comments != null && x.Comments.ToLower().Contains(searchFor));
}
return categories.Where(predicate.Compile()).AsQueryable();
}
专用静态IQueryable筛选器内容(字符串搜索、字符串排除的域、IQueryable类别)
{
if(searchFor.StartsWith(“*”)&&searchFor.EndsWith(“*”))
{
searchFor=searchFor.Substring(1,searchFor.Length-2);
}
searchFor=searchFor.ToLower();
var predicate=PredicateBuilder.False();
如果(!excludedBrFields.Contains(“Title”))
{
谓词=谓词。或(x=>x.Title.ToLower().Contains(searchFor));
}
如果(!excludedBrFields.Contains(“Description”))
{
predicate=predicate.Or(x=>x.Description!=null&&x.Description.ToLower().Contains(searchFor));
}
如果(!excludedBrFields.Contains(“Comments”))
{
predicate=predicate.Or(x=>x.Comments!=null&&x.Comments.ToLower().Contains(searchFor));
}
返回categories.Where(predicate.Compile()).AsQueryable();
}
我最终结合了evanmcdonnal和dasblinkenlight的想法解决了这个问题。我给了他们每人一张赞成票,因为不幸的是,我不能给他们每个帮助回答问题的积分。这就是我最后写的
private static IQueryable<Db.Category> FilterContains(string searchFor, string excludedBrFields, IQueryable<Db.Category> categories)
{
if (searchFor.StartsWith("*") && searchFor.EndsWith("*"))
{
searchFor = searchFor.Substring(1, searchFor.Length - 2);
}
searchFor = searchFor.ToLower();
var predicate = PredicateBuilder.False<Db.Category>();
if (!excludedBrFields.Contains("Title"))
{
predicate = predicate.Or(x => x.Title.ToLower().Contains(searchFor));
}
if (!excludedBrFields.Contains("Description"))
{
predicate = predicate.Or(x => x.Description != null && x.Description.ToLower().Contains(searchFor));
}
if (!excludedBrFields.Contains("Comments"))
{
predicate = predicate.Or(x => x.Comments != null && x.Comments.ToLower().Contains(searchFor));
}
return categories.Where(predicate.Compile()).AsQueryable();
}
专用静态IQueryable筛选器内容(字符串搜索、字符串排除的域、IQueryable类别)
{
if(searchFor.StartsWith(“*”)&&searchFor.EndsWith(“*”))
{
searchFor=searchFor.Substring(1,searchFor.Length-2);
}
searchFor=searchFor.ToLower();
var predicate=PredicateBuilder.False();
如果(!excludedBrFields.Contains(“Title”))
{
谓词=谓词。或(x=>x.Title.ToLower().Contains(searchFor));
}
如果(!excludedBrFields.Contains(“Description”))
{
predicate=predicate.Or(x=>x.Description!=null&&x.Description.ToLower().Contains(searchFor));
}
如果(!excludedBrFields.Contains(“Comments”))
{
predicate=predicate.Or(x=>x.Comments!=null&&x.Comments.ToLower().Contains(searchFor));
}
返回categories.Where(predicate.Compile()).AsQueryable();
}
调用lower-then-pass包含一个以大写字母开头的字符串…该代码永远不会工作。是的,sry,我的意思是供应商是一个变量,而不是硬编码的字符串。这仍然不起作用。如果要进行字符串比较并将其应用于其中一个字符串的下限或上限,则必须将其应用于另一个字符串,则逻辑不健全。这个例子适用于我的程序,具体取决于你查询的数据源……这个查询可能不区分大小写。MySQL就是一个例子,你调用lower-then-pass包含一个以大写字母开头的字符串……这个代码永远不会起作用。是的,sry,我的意思是供应商是一个变量,而不是硬编码字符串。这仍然不起作用。如果您正在进行字符串比较,并将其应用于