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

C#ListContains

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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 - C#

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

Solution 3 - C#

You can using the exists

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

Solution 4 - C#

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 - C#

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

Solution 6 - C#

var item = pricePublicList.FirstOrDefault(x => x.Size == 200);
if (item != null) {
   // There exists one with size 200 and is stored in item now
}
else {
  // There is no PricePublicModel with size 200
}

Solution 7 - C#

You can also just use List.Find():

if(pricePublicList.Find(item => item.Size == 200) != null)
{
    // Item exists, do something
}
else
{
    // Item does not exist, do something else
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionilija veselicaView Question on Stackoverflow
Solution 1 - C#Anthony PegramView Answer on Stackoverflow
Solution 2 - C#Daniel A. WhiteView Answer on Stackoverflow
Solution 3 - C#TiagoView Answer on Stackoverflow
Solution 4 - C#JacobView Answer on Stackoverflow
Solution 5 - C#Mark ByersView Answer on Stackoverflow
Solution 6 - C#Mahesh VelagaView Answer on Stackoverflow
Solution 7 - C#Artorias2718View Answer on Stackoverflow