I'm having trouble creating an IN clause using C # and lambdas.
I have the following GetUserList(string filtersByRoles) method
The variable string filtersByRoles can contain a comma-separated value, such as: "1,2" or "1,2,3" or "1,3,4", etc ... each number represents a unique number of Roles (in other words, RoleId).
Then I have the following request in C #:
var query = _userRepository.GetUserList();
Which returns an IQueryable<User> , where User is the table from my EntityFramework.
As soon as I check if the filtersByRoles parameter is filtersByRoles empty or empty, I need to make an IN clause, for example:
if (!string.IsNullOrEmpty(filtersByRoles)) {
The above code compiles ... but in RUNTIME it crashes with the following error message:
LINQ to Entities does not recognize the method 'Boolean Contains [Int32] (System.Collections.Generic.IEnumerable`1 [System.Int32], Int32)', and this method cannot be translated into the expression store.
I managed to find a workaround, but it involves calling the .ToList() method, which, I believe, extracts all the data from my database, and then adds the Where () clause. But won't this defeat the goal or create some performance issues?
This is what I did:
if (!string.IsNullOrEmpty(filtersByRoles)) { string[] myArray = filtersByRoles.Split(','); query = query.ToList().Where(u => myArray.Contains(u.RoleId.ToString())).AsQueryable(); }
I would prefer not to make a .ToList() call and not retrieve all the data.
Is there any other way to achieve this?
EDIT: I am using Entity Framework 1.0 and .NET Framework 3.5
Thank you sincerely
Vince