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

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

This question is related to c# list contains

The answer is


This is pretty easy to do using LINQ:

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

You can using the exists

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

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

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

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
}

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to contains

Check if element is in the list (contains) Excel formula to search if all cells in a range read "True", if not, then show "False" How to use regex in XPath "contains" function R - test if first occurrence of string1 is followed by string2 Java List.contains(Object with field value equal to x) Check if list contains element that contains a string and get that element Search for "does-not-contain" on a DataFrame in pandas String contains another two strings Java - Opposite of .contains (does not contain) String contains - ignore case