gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:27:25 24 4
gpt4 key购买 nike

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

场景是当我输入一个产品关键字时,我想获取多个类别中的匹配项目编号。

例如,当我输入关键字“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。

最佳答案

您可以使用 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();

关于c# - 如何获取多个类别的关键字匹配数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5013469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com