如何获取多个类别的关键字匹配号?
情况是,当我输入产品关键字时,我希望获得许多类别中的匹配项目编号。
例如,当我输入关键字“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 :(得分: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上执行此操作: - )
答案 1 :(得分: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();