Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6213403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:41:23+00:00 2026-05-24T06:41:23+00:00

I’m having issues creating an IN clause using C# and lambdas. I have the

  • 0

I’m having issues creating an IN clause using C# and lambdas.

I have the following method GetUserList(string filtersByRoles)

The variable string filtersByRoles can hold a comma-delimited value such as: “1,2” or “1,2,3” or “1,3,4” etc…each number represents the unique number of a Role (in other words, RoleId).

I then have the following C# lambda query:

var query = _userRepository.GetUserList();

Which returns an IQueryable<User> where User is a table from my EntityFramework.

Once I verify if the filtersByRoles parameter is not null-or-empty, I need to make an IN clause such as:

if (!string.IsNullOrEmpty(filtersByRoles))
{
   //Convert *filtersByRoles* to an array of integers
   int[] myArray = filtersByRoles.Split(',').Select(x => int.Parse(x)).ToArray();

   //Make the IN clause
   query = query.Where(u => myArray.Contains(u.RoleId));
}

The above code compiles…but at RUNTIME it fails with the following error message:

LINQ to Entities does not recognize the method ‘Boolean
Contains[Int32](System.Collections.Generic.IEnumerable`1[System.Int32],
Int32)’ method, and this method cannot be translated into a store
expression.


I’ve manage to find a workaround but it involves making a call to the .ToList() method which I believe fetches all the data from my database and then, adds a Where() clause.
But wouldn’t that defeat the purpose or create some performance issues?

This is what I’ve done:

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 the .ToList() call and avoid fetching all the data.

Is there another way to achieve this?

EDIT:
I’m using Entity Framework 1.0 and .NET Framework 3.5

Thanks
Sincerely

Vince

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:41:24+00:00Added an answer on May 24, 2026 at 6:41 am

    Here are my 2 cents:

    Maybe the Dynamic LinQ will help solve your problem:
    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    You could build your Where clause as a string, say something like :

    string sWhereClause = "1 = 1";
    foreach(string rId in filtersByRoles.Split(','))
       sWhereClause += " OR RoleId = " + rId;
    

    (I would suggest to use StringBuilder instead of +concat, but for the purpose of this answer, it doesn’t matter)

    and then

    query = query.Where(sWhereClause);
    

    I haven’t tryed it though, but it sounds fair for solving your problem. Even though it looks like SQL injection… Well, improvements can be brought.

    EDIT:
    As a second thought I manage to come with this new idea:

    string filterByRoles = "1,2,3";
    query = query.Where(new Func<User, bool>(u => {
       return filterByRoles.Contains(u.RoleId.ToString());
    })).AsQueryable();
    

    This way, you can add whatever code you want in the Func{ … } delegate, as long as it returns a boolean (I assumed here your TInput was a “User” class, of course change it to use the one corresponding to you needs).

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having issues creating an ActionLink using Preview 5. All the docs I can
I'm having some issues creating a string using 'initWithFormat'. Here is the code I'm
I'm using Fluent Mongo and having issues creating a dynamic linq query because Fluent
After following the official tutorial here: tutorial I'm still having issues adding rows/creating a
I'm creating a database using entity framework code first and I'm having some issues
I'm having some issues with producing an int matrix without creating memory leaks. I
I am having some issues with managers, because they believe that (intuitively) creating desktop
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
I'm having issues with my SQL Reporting Services reports. I'm using a custom font

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.