I have an array of string values, now what I need is to filter the list of data based on matching each element of array with list, means need to select those list items which has typeId == SomeValueInTypeArray
Something like this
//array created by splitting string
var TYPES = types.Split(',');
//Select those list items which has typeId == SomeValueInTypeArray
list = context.CompanyUsers.Where(a => a.TypeId.IsIn(Types)).ToList();
How can I do this using lambda?
I am using Entity Framework in MVC project
Answered by:- vikas_jk
You can find element in list using .Contains() or .Any() method in C#, so your code will be like this
var TYPES = types.Split(',');
//Select those list items which has typeId == SomeValueInTypeArray
list = context.CompanyUsers.Where(a => TYPES.Any(type => type == a.TypeId)).ToList();
Answered by:- jon
In the above answer, there is one way of .Any(), seconf way is that you can use .Contains() to check if array item exists in your List or not
Using C# code like below
var matchingvalues = myList.Where(stringToCheck => stringToCheck.Contains(myString));
Answered by:- bhanu
If you just want to search for a "string" in a list, you can also test it using "Exists", here is the sample
List<string> list1 = new List<string>(){
"Welcome",
"to",
"World"
};
if(list1.Exists(x => x == "World"))
{
Console.WriteLine("Item Exists");
}
else
{
Console.WriteLine("Item doesn't Exists");
}
Output:
Item Exists
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly