how to check if List<T> element contains an item with a Particular Property Value
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);
ilija veselica
Updated on June 22, 2021Comments
-
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 existspricePublicModel.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 about 13 yearsThis is strictly to see if one exists.
-
George Johnston about 13 yearsThis doesn't really answer the question. He said,
how to know which one is it
-
ilija veselica about 13 yearsIs it possible to directly edit the element that matches this condition? Not to get it and store in new object, but change directly?
-
Jacob about 13 yearsThe
match
value that is returned can be modified (it is thePricePublicModel
object that was in the list). -
Amy West about 13 yearsYou should not directly modify an object that can be null. You can always do:
match.Size = 300;
orforeach(var item in pricePublicList.Where(p => p.Size == 200)) item.Size = 300;
. Would not use it as a one-liner myself, though. -
Igor about 10 yearsMight 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 over 9 yearswhat 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 almost 9 yearsIt 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 almost 9 yearsIf anyone tries this and .Any() is unrecognised, you need to add using System.Linq; at the top of your file.
-
Shaiju T about 8 years
.Any
or.FindIndex
which is fast ? -
BenKoshy about 7 yearswhat if the item doesn't exist: what will the index value be?
-
tomloprod about 7 yearsNOTE: If the element doesn't exist,
findIndex
will return–1
. Documentation: msdn.microsoft.com/es-es/library/x1xzf2ca(v=vs.110).aspx -
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 about 6 yearsThis one helps me too. For my case I don't care which one it is, just whether it exists or not.