2011-02-16 40 views
1

如何獲取多個類別的關鍵字匹配號碼?如何獲取多個類別的關鍵字匹配號碼?

這種情況是,當我輸入一個產品關鍵字時,我想獲得許多類別中的匹配項目編號。

例如,當我鍵入關鍵詞「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。

回答

0

你能做到這一點使用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(); 
0

如果你有

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 :-)

+0

但是,關鍵字是未知的,客戶可能輸入任何關鍵字,那麼它應該顯示包含關鍵字的每個產品類別的每個匹配號碼。 – Mike108 2011-02-16 11:07:11

+0

???你能寫出你的對象的類和他們的關係嗎?你是否需要它在內存或數據庫? – xanatos 2011-02-16 11:56:05