C# 通过T2[]在T1[]中查找T1的有效方法,其中T2[]是T1上的属性?
根据给定示例中的属性查找产品的快速方法是什么?我有一个3000个产品的列表,每个产品都有一个12个属性对象的列表,如示例中所示。我需要能够找到产品快速使用n个属性C# 通过T2[]在T1[]中查找T1的有效方法,其中T2[]是T1上的属性?,c#,C#,根据给定示例中的属性查找产品的快速方法是什么?我有一个3000个产品的列表,每个产品都有一个12个属性对象的列表,如示例中所示。我需要能够找到产品快速使用n个属性 public class Test { public class ProductProperty { public string Name { get; set; } public string Value { get; set; } public ProductProp
public class Test
{
public class ProductProperty
{
public string Name { get; set; }
public string Value { get; set; }
public ProductProperty() { }
public ProductProperty(string name, string value)
{
this.Name = name;
this.Value = value;
}
}
public class Product
{
public string ProductName { get; set; }
public ProductProperty[] Properties { get; set; }
}
public static void Main(string[] args)
{
List<Product> models = new List<Product>()
{
new Product() { ProductName = "Test1", Properties = new ProductProperty[] { new ProductProperty("title", "car"), new ProductProperty("length", "5") } },
new Product() { ProductName = "Test1", Properties = new ProductProperty[] { new ProductProperty("title", "car"), new ProductProperty("length", "7") } },
new Product() { ProductName = "Test1", Properties = new ProductProperty[] { new ProductProperty("title", "ship"), new ProductProperty("length", "9") } },
};
var findByProps = new ProductProperty[] { new ProductProperty("title", "car"), new ProductProperty("length", "7") };
// var product = find Product that has title=car and length=7
}
}
公共类测试
{
公共类ProductProperty
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
public ProductProperty(){}
公共产品属性(字符串名称、字符串值)
{
this.Name=Name;
这个。值=值;
}
}
公共类产品
{
公共字符串ProductName{get;set;}
public ProductProperty[]属性{get;set;}
}
公共静态void Main(字符串[]args)
{
列表模型=新列表()
{
new Product(){ProductName=“Test1”,Properties=new ProductProperty[]{new ProductProperty(“title”,“car”),new ProductProperty(“length”,“5”)},
new Product(){ProductName=“Test1”,Properties=new ProductProperty[]{new ProductProperty(“title”,“car”),new ProductProperty(“length”,“7”)},
new Product(){ProductName=“Test1”,Properties=new ProductProperty[]{new ProductProperty(“title”,“ship”),new ProductProperty(“length”,“9”)},
};
var findByProps=newproductproperty[]{newproductproperty(“title”,“car”),newproductproperty(“length”,“7”)};
//var product=查找标题为car且长度为7的产品
}
}
如果在ProductProperty
中重写Equals方法:
public override bool Equals(object o) => o is ProductProperty p && p.Name == Name && p.Value== Value;
比较ProductProperty更容易(您也可以实现IEquatable)。(注意,较旧的Visual Studio不支持上述语法,但如果需要,可以轻松重写)
一旦被重写,可以使用任何默认方法,例如Contains:
var product = models.FirstOrDefault(m=> findByProps.All(m.Properties.Contains));
如果在
ProductProperty
中重写Equals方法:
public override bool Equals(object o) => o is ProductProperty p && p.Name == Name && p.Value== Value;
比较ProductProperty更容易(您也可以实现IEquatable)。(注意,较旧的Visual Studio不支持上述语法,但如果需要,可以轻松重写)
一旦被重写,可以使用任何默认方法,例如Contains:
var product = models.FirstOrDefault(m=> findByProps.All(m.Properties.Contains));
如果只有一次,则操作
FirstOrDefault
将起作用。如果你想多次这样做,我建议你对你的收藏进行分类,并使用二进制搜索。这很有趣!请详细说明“m=>findByProps.All(m.Properties.Contains)”?什么作为参数传递到Contains中?findByProps中的每个元素?是否与写入“m=>findByProps.All(prop=>m.Properties.Contains(prop))”相同?是的,是相同的,但没有额外的lambda。属性数组实例的Contains
方法直接传递给All
(扩展)方法,因此All
中的每个迭代都可以直接调用它,如果它只是一次动作FirstOrDefault
将起作用。如果你想多次这样做,我建议你对你的收藏进行分类,并使用二进制搜索。这很有趣!请详细说明“m=>findByProps.All(m.Properties.Contains)”?什么作为参数传递到Contains中?findByProps中的每个元素?是否与写入“m=>findByProps.All(prop=>m.Properties.Contains(prop))”相同?是的,是相同的,但没有额外的lambda。属性数组实例的Contains
方法直接传递给All
(扩展)方法,因此All