我有一个非常简单的谓词构建器查询,效果很好。我们有一个要搜索的关键字列表,我们遍历这些关键字以查看我们的任何对象属性是否匹配。
var predicateSearchText = PredicateBuilder.False<Asset>();
List<string> separatedKeywords = keywords.Split(',').ToList();
foreach (string s in separatedKeywords)
{
if (s.Length > 0)
{
string temp = s;
predicateSearchText = predicateSearchText.Or(m => m.Notes.Contains(temp));
predicateSearchText = predicateSearchText.Or(m => m.Description.Contains(temp));
predicateSearchText = predicateSearchText.Or(m => m.DetailedDescription.Contains(temp));
predicateSearchText = predicateSearchText.Or(asset => asset.Keywords.Contains(temp));
predicateSearchText = predicateSearchText.Or(a => a.AssetFiles.Any(x => x.FileName.Contains(temp)));
}
}
......我做得更进一步:
var query = (from a in masterQuery.Where(predicateSearchText);
我得到了我需要的东西 - 只过滤那些我搜索过的关键词。
我需要更改此设置,以便查找实体的某个属性中的所有关键字。所以“notes”字段必须要搜索每个关键字,或者“description”字段必须包含所有要搜索的关键字。我认为这涉及内部/外部谓词,但我无法想出它。我试过的东西给了我太多的结果,它应该只给我一个...其他实体与标准不符。我的问题显而易见吗?我的关键字(其中6个)非常独特,我回来了600多个项目,而不是1 ...
var predicateSearchText = PredicateBuilder.False<Asset>();
var notesPred = PredicateBuilder.True<Asset>();
var descPred = PredicateBuilder.True<Asset>();
var detdescPred = PredicateBuilder.True<Asset>();
var keywordPred = PredicateBuilder.True<Asset>();
var filesPred = PredicateBuilder.True<Asset>();
foreach (string s in separatedKeywords.Where(xx=>xx.Length > 0))
descPred.And(m => m.Description.Contains(s));
foreach (string s in separatedKeywords.Where(xx => xx.Length > 0))
notesPred.And(m => m.Notes.Contains(s));
foreach (string s in separatedKeywords.Where(xx => xx.Length > 0))
detdescPred.And(m => m.DetailedDescription.Contains(s));
foreach (string s in separatedKeywords.Where(xx => xx.Length > 0))
keywordPred.And(m => m.Keywords.Contains(s));
foreach (string s in separatedKeywords.Where(xx => xx.Length > 0))
filesPred.And(a => a.AssetFiles.Any(x => x.FileName.Contains(s)));
predicateSearchText = predicateSearchText.Or(notesPred.Expand());
predicateSearchText = predicateSearchText.Or(descPred.Expand());
predicateSearchText = predicateSearchText.Or(detdescPred.Expand());
predicateSearchText = predicateSearchText.Or(keywordPred.Expand());
predicateSearchText = predicateSearchText.Or(filesPred.Expand());
然后我做我的:
var query = (from a in masterQuery.Where(predicateSearchText);
我得到的结果太多,谓词是正确的。
答案 0 :(得分:0)
我今天遇到了同样的问题。在循环内部移动PredicateBuilder实例化和Where()函数,它将有效地和单独的关键字谓词。
var predicateSearchText;
List<string> separatedKeywords = keywords.Split(',').ToList();
foreach (string s in separatedKeywords)
{
if (s.Length > 0)
{
predicateSearchText = PredicateBuilder.False<Asset>();
string temp = s;
predicateSearchText = predicateSearchText.Or(m => m.Notes.Contains(temp));
predicateSearchText = predicateSearchText.Or(m => m.Description.Contains(temp));
predicateSearchText = predicateSearchText.Or(m => m.DetailedDescription.Contains(temp));
predicateSearchText = predicateSearchText.Or(asset => asset.Keywords.Contains(temp));
predicateSearchText = predicateSearchText.Or(a => a.AssetFiles.Any(x => x.FileName.Contains(temp)));
Query = Query.AsExpandable().Where(predicateSearchText);
}
}