Check if list contains a value using lambda and EF in MVC


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


Asked by:- Vinnu
0
: 810 At:- 1/4/2018 1:45:18 PM
C# lambda lambda expression contains list how to use select in lambda expression linq






2 Answers
profileImage 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();
2
At:- 1/5/2018 10:40:46 AM Updated at:- 3/11/2018 7:18:12 PM


profileImage 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));
1
At:- 1/9/2018 11:44:19 AM Updated at:- 1/9/2018 11:44:35 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use