使用lambda进行多个关键字搜索
本文关键字:关键字 搜索 lambda 使用 | 更新日期: 2023-09-27 18:05:33
这件事我已经纠结了一天了,进展甚微。
我有一些产品,我希望能够通过关键字和其他静态变量进行过滤。任何人都可以在搜索栏中输入这些关键字。我被困的地方是当有人输入多个关键字。如果只有一个关键字,查询工作很好。
如何搜索与一个项目相关的多个关键字,并且只返回与关键字匹配的项目?
示例关键词:帐篷,蓝色,徒步旅行
public static List<ProductObjectModel> GetProductsByKeyword(string keywords)
{
using (TraegerEntities db = new TraegerEntities())
{
List<ProductObjectModel> lstObj = new List<ProductObjectModel>();
if (!string.IsNullOrEmpty(keywords))
{
string[] keyword = keywords.Split(',');
var lstAnon = (
from r in db.Products
join i in db.ProductImages on r.ProductId equals i.ProductId
join c in db.ProductCategories on r.ProductId equals c.ProductId
join cl in db.ProductCategoryList on c.ProductCategoryListId equals cl.ProductCategoryListId
join k in db.ProductKeywords on r.ProductId equals k.ProductId
join kl in db.ProductKeywordList on k.ProductKeywordListId equals kl.ProductKeywordListId
where r.Archive == false
where i.SmallImage == true
where keyword.Any(kw => kl.Keyword.Contains(kw))
group new
{
Products = r,
ProductCategoryList = cl,
ProductImages = i,
ProductKeywordList = kl
} by r.ProductId
).ToList();
}
foreach (var item in lstAnon.Skip(beginRange).Take(endRange))
{
ProductObjectModel obj = new ProductObjectModel();
obj.ProductDetails = item.First().Products;
obj.CategoryList = item.First().ProductCategoryList;
obj.Images = item.First().ProductImages;
obj.KeywordList = item.First().ProductKeywordList;
lstObj.Add(obj);
}
return lstObj;
}
}
如果我没理解错的话,
当用户插入"Tent, Blue, Hiking"
时,您想要显示包含这三个关键字的所有项吗?
如果是这种情况,简单地从.Any()
交换到.All
应该可以帮助您。
Like so: where keyword.All(kw => kl.Keyword.IndexOf(kw, StringComparison.OrdinalIgnoreCase) >= 0)
和修剪你的东西:
var keyword = keywords.Split(',').Select(y => y.Trim());
经过一周的努力,我终于找到了解决这个问题的办法。通过比较两个列表,我能够得到我需要的结果。这篇文章是我的解决方案。
public static List<ProductObjectModel> GetProductsByKeyword(string keywords)
{
using (TraegerEntities db = new TraegerEntities())
{
List<ProductObjectModel> lstObj = new List<ProductObjectModel>();
if (!string.IsNullOrEmpty(keywords))
{
string[] keyword = keywords.Split(',');
List<ProductObjectModel> lstAnon = (
from r in db.Products
join i in db.ProductImages on r.ProductId equals i.ProductId
join c in db.ProductCategories on r.ProductId equals c.ProductId
join cl in db.ProductCategoryList on c.ProductCategoryListId equals cl.ProductCategoryListId
join k in db.ProductKeywords on r.ProductId equals k.ProductId
join kl in db.ProductKeywordList on k.ProductKeywordListId equals kl.ProductKeywordListId
where r.Archive == false
where i.SmallImage == true
select new ProductObjectModel
{
Products = r,
CategoryList = cl,
KeywordList = kl,
Images = i
}
).ToList();
var lstFiltered = lstAnon;
List<string> keywordList = new List<string>(keywords.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));
foreach (string word in keywordList)
{
var tempList = lstAnon.Where(r => r.KeywordList.Keyword == word).ToList();
lstFiltered = lstFiltered.Intersect(tempList, new MyEqualityComparer()).ToList();
}
foreach (var item in lstAnon.Skip(beginRange).Take(endRange))
{
ProductObjectModel obj = new ProductObjectModel();
obj.Products = item.Products;
obj.CategoryList = item.ProductCategoryList;
obj.Images = item.ProductImages;
obj.KeywordList = item.ProductKeywordList;
lstObj.Add(obj);
}
}
return lstObj;
}
}
public class MyEqualityComparer : IEqualityComparer<ProductObjectModel>
{
public bool Equals(ProductObjectModel x, ProductObjectModel y)
{
return x.ProductObjectModel.ProductId == y.ProductObjectModel.ProductId;
}
public int GetHashCode(ProductObjectModelobj)
{
return obj.Products.ProductId.GetHashCode();
}
}