Reputation: 201
I have list as follows:
readonly List<string> carMake = new List<string>
{
"Toyota",
"Honda",
"Audi",
"Tesla"
};
I have a string at runtime which is as follows:
string strOutput = "Project1:Toyota:Corolla";
Now, I will like to use strOutput and carMake to make sure the string as correct car make. How do I do this using Linq?
I want to:
Upvotes: 0
Views: 73
Reputation: 2857
Use the Any() method with a predicate to check if any of the strings in the carMake
list is contained inside the strOutput
:
return carMake.Any(i => strOutput.Contains(i));
OR, if your runtime string will always be in that format, you can split by ':' and compare to the value in the middle:
string runtimeValue = strOutput.Split(':')[1];
return carMake.Contains(runtimeValue);
Upvotes: 1
Reputation: 17855
List<string> carMake = new List<string> { "Toyota", "Honda", "Audi", "Tesla" };
string strOutput = "Project1:Toyota:Corolla";
string carBrand = strOutput.Split(':')[1];
bool result = carMake.Contains(carBrand);
Upvotes: 0