如何检查 List 元素是否包含具有特定属性值的项目

2024-03-21

public class PricePublicModel
{
    public PricePublicModel() { }

    public int PriceGroupID { get; set; }
    public double Size { get; set; }
    public double Size2 { get; set; }
    public int[] PrintType { get; set; }
    public double[] Price { get; set; }
}

List<PricePublicModel> pricePublicList = new List<PricePublicModel>();

如何检查元素是否为pricePublicList含有一定的价值。更准确地说,我想检查是否存在pricePublicModel.Size == 200?另外,如果这个元素存在,如何知道它是哪一个呢?

编辑如果字典更适合这个那么我可以使用字典,但我需要知道如何:)


如果您有一个列表,并且想知道列表中的哪个位置存在与给定条件匹配的元素,则可以使用FindIndex实例方法。例如

int index = list.FindIndex(f => f.Bar == 17);

Where f => f.Bar == 17是具有匹配条件的谓词。

在你的情况下你可能会写

int index = pricePublicList.FindIndex(item => item.Size == 200);
if (index >= 0) 
{
    // element exists, do what you need
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检查 List 元素是否包含具有特定属性值的项目 的相关文章

随机推荐