【问题标题】:How to get the keyword match number for many categories?如何获取多个类别的关键字匹配数?
【发布时间】:2011-02-16 07:06:25
【问题描述】:

如何获取多个类别的关键字匹配数?

场景是,当我输入一个产品关键字时,我想得到很多类别中的匹配项号。

例如,当我输入关键字“iphone”时,页面会在很多类别中显示匹配的项目编号:

Mobile(5)
battery(2)
app(6)
typeA(2)
typeB(9)
typeC(15)
typeC(1)
typeD(9)
typeE(7)
typeF(8)
......
......
typeZ(5)



public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
}

public class Type
{
    public int TypeId { get; set; }
    public string TypeName { get; set; }
}

public class ProductType
{
    public int ProductId { get; set; }
    public int TypeId { get; set; }
}

/// <summary>
/// Test Data
/// </summary>
public class TestData
{
    public List<Product> GetProductList()
    {
        var list = new List<Product>(){
             new Product(){ ProductId=1, ProductName = "iphone1"},
             new Product(){ ProductId=2, ProductName = "iphone2"},
             new Product(){ ProductId=3, ProductName = "iphone3"},
             new Product(){ ProductId=4, ProductName = "ipad1"},
             new Product(){ ProductId=5, ProductName = "ipad2"},
             new Product(){ ProductId=6, ProductName = "mobile1"},
             new Product(){ ProductId=7, ProductName = "mobile2"},
             new Product(){ ProductId=8, ProductName = "cpu1"},
             new Product(){ ProductId=9, ProductName = "cpu2"},
             new Product(){ ProductId=10, ProductName = "cpu3"}
            };
        return list;
    }

    public List<Type> GetTypeList()
    {
        var list = new List<Type>(){
             new Type(){ TypeId=1, TypeName = "type1"},
             new Type(){ TypeId=2, TypeName = "type2"},
             new Type(){ TypeId=3, TypeName = "type3"},
             new Type(){ TypeId=4, TypeName = "type4"},
             new Type(){ TypeId=5, TypeName = "type5"}         
            };
        return list;
    }

    public List<ProductType> GetProductTypeList()
    {
        var list = new List<ProductType>(){
             new ProductType(){ ProductId=1, TypeId=1},
             new ProductType(){ ProductId=1, TypeId=2},
             new ProductType(){ ProductId=2, TypeId=1},
             new ProductType(){ ProductId=2, TypeId=3},
             new ProductType(){ ProductId=2, TypeId=4},
             new ProductType(){ ProductId=3, TypeId=2},
             new ProductType(){ ProductId=3, TypeId=5},
             new ProductType(){ ProductId=4, TypeId=2},
             new ProductType(){ ProductId=4, TypeId=3},
             new ProductType(){ ProductId=4, TypeId=5},
             new ProductType(){ ProductId=5, TypeId=2},
             new ProductType(){ ProductId=5, TypeId=4},
             new ProductType(){ ProductId=6, TypeId=1},
             new ProductType(){ ProductId=6, TypeId=2},
             new ProductType(){ ProductId=7, TypeId=2},
             new ProductType(){ ProductId=7, TypeId=5},
             new ProductType(){ ProductId=8, TypeId=2},
             new ProductType(){ ProductId=9, TypeId=3},
             new ProductType(){ ProductId=10, TypeId=2}
            };
        return list;
    }

如何实现这一点以获得更好的性能?

我使用 C# ASP.NET。

【问题讨论】:

    标签: c# algorithm search


    【解决方案1】:

    您可以使用 LINQ 执行此操作 - 现在不是很干净,但它可以为您提供一个列表 TypeMatches,其中包含类型名称和该类型的匹配数。

    TestData testData = new TestData();
    string keyword = "iphone";
    var ProductListMatches = testData.GetProductList().Where(p => p.ProductName.Contains(keyword)).ToList();
    
    var ProductTypeMatches = (from product in ProductListMatches
                                join productType in testData.GetProductTypeList() 
                                on product.ProductId equals productType.ProductId
                                select productType).ToList();
    
    var TypeMatches = (from productTypeMatch in ProductTypeMatches
                        join pType in testData.GetTypeList()
                            on productTypeMatch.TypeId equals pType.TypeId
                        select pType).GroupBy(p => p.TypeId)
                        .Select(g => new { TypeName =  g.First().TypeName, Count = g.Count() })
                        .ToList();
    

    【讨论】:

      【解决方案2】:

      如果你有一个

      public class Item {
          public string Name;
          public string Keyword;
          public int Num;
      }
      

      List<Item> items = new List<Item>();
      

      然后

      var items2 = items.ToLookup(p => p.Keyword);
      

      您可以像这样搜索关键字:

      foreach (var item in items2[Keyword]) {
      }
      

      但也许您想要的不仅仅是每个项目的关键字......

       public class Item
       {
           public string Name;
           public List<string> Keywords = new List<string>();
           public int Num;
       }
      
       List<Item> items = new List<Item>();
       var items2 = items.SelectMany(p => p.Keywords.Select(q => new { Keyword = q, Item = p })).ToLookup(p => p.Keyword, p => p.Item);
      

      和以前一样使用。

      除非你想在 SQL 上做 :-)

      【讨论】:

      • 但是关键字是未知的,客户可以输入任何关键字,然后它应该显示每个包含该关键字的产品类别的匹配数。
      • ???你能写出你的对象的类和它们的关系吗?您需要它在内存中还是在数据库中?
      • 我已经编辑了问题并添加了示例类及其关系。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多