how to check if List<T> element contains an item with a Particular Property Value

285,246

Solution 1

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);

Where f => f.Bar == 17 is a predicate with the matching criteria.

In your case you might write

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

Solution 2

bool contains = pricePublicList.Any(p => p.Size == 200);

Solution 3

You can using the exists

if (pricePublicList.Exists(x => x.Size == 200))
{
   //code
}

Solution 4

This is pretty easy to do using LINQ:

var match = pricePublicList.FirstOrDefault(p => p.Size == 200);
if (match == null)
{
    // Element doesn't exist
}

Solution 5

You don't actually need LINQ for this because List<T> provides a method that does exactly what you want: Find.

Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>.

Example code:

PricePublicModel result = pricePublicList.Find(x => x.Size == 200);
Share:
285,246
ilija veselica
Author by

ilija veselica

Updated on June 22, 2021

Comments

  • ilija veselica
    ilija veselica almost 3 years
    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>();
    

    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 :)

  • Daniel A. White
    Daniel A. White about 13 years
    This is strictly to see if one exists.
  • George Johnston
    George Johnston about 13 years
    This doesn't really answer the question. He said, how to know which one is it
  • ilija veselica
    ilija veselica about 13 years
    Is it possible to directly edit the element that matches this condition? Not to get it and store in new object, but change directly?
  • Jacob
    Jacob about 13 years
    The match value that is returned can be modified (it is the PricePublicModel object that was in the list).
  • Amy West
    Amy West about 13 years
    You should not directly modify an object that can be null. You can always do: match.Size = 300; or foreach(var item in pricePublicList.Where(p => p.Size == 200)) item.Size = 300;. Would not use it as a one-liner myself, though.
  • Igor
    Igor about 10 years
    Might not answer his question, but it sure did help me, because I needed to see if an object with a given name already exists in my collection. I expected an improved .Contains-method - and this is exactly what this LINQ-expression does. Thank you.
  • Haithem KAROUI
    Haithem KAROUI over 9 years
    what if i don't know the attribute of that value? e.g. i have a list item that is displaying "18.00" i don't know where it is coming from so i want to check wich attribute has this value? so basically , and similirally to this example i don't have the .size or .bar i want to figure it out
  • MGOwen
    MGOwen almost 9 years
    It answers the Question in the title. OP explains in his description that he doesn't really want exactly what he said he wants. Still, there's one of him, and dozens of us finding this question through google. I think it's more important that we be able to find the real answer to his title question.
  • MGOwen
    MGOwen almost 9 years
    If anyone tries this and .Any() is unrecognised, you need to add using System.Linq; at the top of your file.
  • Shaiju T
    Shaiju T about 8 years
    .Any or .FindIndex which is fast ?
  • BenKoshy
    BenKoshy about 7 years
    what if the item doesn't exist: what will the index value be?
  • tomloprod
    tomloprod about 7 years
    NOTE: If the element doesn't exist, findIndex will return –1. Documentation: msdn.microsoft.com/es-es/library/x1xzf2ca(v=vs.110).aspx
  • DannyC
    DannyC over 6 years
    @tomloprod that's why he checks for an index greater than or equal to zero. if (index >= 0). Could have also written it as if (index > -1).
  • afterxleep
    afterxleep about 6 years
    This one helps me too. For my case I don't care which one it is, just whether it exists or not.