[英]comparison of the list in the list
我有一個清單清單:
List<Product> productList = new List<Product>()
{
new Product()
{
Id = 1,
Model = "Phone",
TypeProd = new CheckTypes
{
ChTypes = new List<CHType>
{
new CHType
{
Id = 8,
IdName = "261"
},
new CHType
{
Id = 9 ,
IdName = "149"
}
}
}
},
new Product
{
Id = 1,
Model = "Printer",
TypeProd = new CheckTypes
{
ChTypes = new List<CHType>
{
new CHType
{
Id = 8,
IdName = null
},
new CHType
{
Id = 8,
IdName = "261"
}
}
}
}
};
我想通過將IdName
元素與string[]
進行比較來獲得此列表的第一項:
string[] arrStr = new string[] { "261", "149" };
我該如何做得更好? 嘗試使用foreach並創建一個采用數組值的臨時對象,然后使用相交進行比較。
您可以使用LINQ輕松完成此操作:
var product = productList
.FindAll(x => x.TypeProd.ChTypes
.All(y => arrString.Contains(y.IdName));
這將為您提供TypeProd.ChTypes
元素都在arrString
所有產品。
為了獲得更快的性能,您可能需要將arrString
轉換為HashSet<string>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.