Home > DeveloperSection > Forums > Find in List<t> and return boolean if found
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    January-29-2014 11:26 PM

 C# C# 
Ratings:


 1 Reply(s)
 1131  View(s)
Rate this:

public class MyType

{

    public string MyName;

    public string MyTown;

    public string MyJob;

}

I have a List of this type:

List<MyType> myVar = new List<MyType>()

Now I want to return a boolean based on this. So for example: Does myVar contain MyName = "Bob" and MyJob = "Taxi Driver"

I have managed to return all with a name of "Bob"

var e = myVar.Where(x => x.MyName== "Bob").ToList();

But I want to do something like:

bool mExists = (myVar.Where(x => x.MyName== "Bob" && MyJob="Taxi Driver").Count > 0) ? true : false;

Is this possible?



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-29-2014 11:48 PM

Hi Jay,

You can use .Any()

Example :

bool mExists = myVar.Any(x => x.MyName == "Steve" && x.MyJob == "Mechanic");

Don't want to miss updates? Please click the below button!

Follow MindStick