'Check if object in list has specific field content
Sorry for question title, I don't know really how to explain it in a single sentence...
I have a class like this
public class xyz
{
public static string attr1;
public static string attr2;
public static string attr3;
}
how can i check if there is an object with attr1=="aaa" in a List<xyz>?
is there something like
List<xyz> MyList = new List<xyz>();
[...]
bool attr1_exist = MyList.attr1.Contains("aaa");
?
Solution 1:[1]
Solution 2:[2]
Try Following Code:
Boolean IsExist = MyList.Any(n=> n.attr1 == "aaa");
OR
if(MyList.Where(n=> n.attr1 == "aaa").Count() > 0)
{
//Do Somthing . . .
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DGibbs |
| Solution 2 | AliNajafZadeh |
