如果list&LT如何检查; T>元素包含特定属性值的项目 [英] how to check if List<T> element contains an item with a Particular Property Value

查看:122
本文介绍了如果list&LT如何检查; T>元素包含特定属性值的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ?此外,如果该元素存在,怎么知道它是哪一个?

How to check if element of pricePublicList contains certain value. To be more precise, I want to check if there exists pricePublicModel.Size == 200? Also, if this element exists, how to know which one it is?

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

EDIT If Dictionary is more suitable for this then I could use Dictionary, but I would need to know how :)

推荐答案

如果你有一个列表,你想知道在哪里列表中的元素存在一个匹配给定条件,你可以使用 FindIndex 实例方法。如

If you have a list and you want to know where within the list an element exists that matches a given criteria, you can use the FindIndex instance method. Such as

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



其中, F => f.Bar == 17 与匹配条件的谓语。

在你的情况,你可以写

int index = pricePublicList.FindIndex(item => item.Size == 200);
if (index >= 0) 
{
    // element exists, do what you need
}

这篇关于如果list&LT如何检查; T&GT;元素包含特定属性值的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆