使用lambda进行多个关键字搜索

时间:2014-05-28 19:32:18

标签: c# linq entity-framework lambda

我被困在这一天已经取得了很大的进展。

我希望能够按关键字和其他静态变量过滤产品。任何人都可以在搜索栏中输入这些关键字。我遇到困难的地方是有人输入多个关键字。如果只有一个关键字,则查询效果很好。

如何搜索与项目相关的多个关键字,并仅返回与关键字匹配的项目?

示例关键字:帐篷,蓝色,徒步旅行

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;
    }
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确的话,

当用户插入"Tent, Blue, Hiking"时,您是否要显示包含这三个关键字的所有项目?

如果是这种情况,从.Any().All的简单交换可以帮助您。

像这样:where keyword.All(kw => kl.Keyword.IndexOf(kw, StringComparison.OrdinalIgnoreCase) >= 0)

并修剪你的东西:

var keyword = keywords.Split(',').Select(y => y.Trim());

答案 1 :(得分:0)

在解决这个问题一周后,我终于找到了解决方案。通过比较两个列表,我能够得到我需要的结果。 This post是我的解决方案。

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();
    }
}