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 647173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:41:36+00:00 2026-05-13T21:41:36+00:00

I am using LINQ to SQL and I am allowing users to set up

  • 0

I am using LINQ to SQL and I am allowing users to set up the query via assigning values to queryStrings in the UI. I set up the primary query to return an IQueryable result and then keep refining the results set by continuing to act upon the resulting IQueryable object. Everything works fine and the code looks similar to this

var result = (from record in db.Companies
select new Company
{
     Id = record.Id,
     Name = record.Name,
     City = record.City,
     Status = record.Status
});
if (queryName != null && queryName!= "")
{
  result = result.Where(p => p.Name.Contains(queryName));
}

if (queryCity != null && queryCity!= "")
{
   result = result.Where(p => p.City.StartsWith(queryCity));
}

Now I want to extend the query my getting matching a set of elements similar to “IN CLAUSE” in SQL. Where there is a list of elements to use in the query e.g.

string[] queryStatusList = {"x", "y" };

And now I can write the code like this and everything is still OK.

var result = (from record in db.Companies
where queryStatusList.Contains(record.status)
   select new Company
   {
      Id = record.Id,
      Name = record.Name,
      City = record.City,
      Status = record.Status
   });
if (queryName != null && queryName!= "")
{
  result = result.Where(p => p.Name.Contains(queryName));
}

if (queryCity != null && queryCity!= "")
{
   result = result.Where(p => p.City.StartsWith(queryCity));
}

But, I don’t want to have a where clause in the initial query. I want to build from the refined query result as done in the previous example. My question is how would I structure such a query. I tried

if (queryStatusList != null && queryStatusList.Count() > 0)
{
    result = result.Where(queryStatusList.Contains(result.Select(p => p.Status.ToString())));
}

But I get a compiler error: “The type arguments for method ‘System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)’ cannot be inferred from the usage. Try specifying the type arguments explicitly” I have tried a few variations but I’m not sure how to fix the issue.

  • 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-13T21:41:37+00:00Added an answer on May 13, 2026 at 9:41 pm

    I think if you change it slightly, it may work:

    if (queryStatusList != null && queryStatusList.Count() > 0) 
    { 
        result = result.Where( r=> queryStatusList.Contains( r.Status )); 
    }
    

    On the other hand, you might want to look at using a PredicateBuilder to build up a single Where clause selector and use it instead. The PredicateBuilder will give you more control and the ability to create complex queries with a mix of AND and OR clauses while still building them up dynamically.

    var predicate = PredicateBuilder.True<Company>();
    
    if (queryName != null && queryName!= "") 
    { 
        predicate = predicate.And( p => p.Name.Contains(queryName) );
    } 
    
    if (queryCity != null && queryCity!= "") 
    { 
       predicate = predicate.And(p => p.City.StartsWith(queryCity)); 
    }
    
    if (queryStatusList != null && queryStatusList.Count() > 0) 
    { 
        predicate = predicate.And( p => queryStatusList.Contains( p.Status )); 
    }
    
    var result = db.Companies
                   .Select( c => new Company 
                    { 
                        Id = record.Id, 
                        Name = record.Name, 
                        City = record.City, 
                        Status = record.Status 
                    }
                   .Where( predicate );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Linq-to-sql as an ORM. I wrote this innerjoin public IQueryable<Material> FindAllMaterials()
I am using linq to sql. I am running a query which uses a
Using Linq to SQL How does one query and match to include all rows
I am using Linq to SQl to query the data. When I write a
Using Linq to Sql how do I group the following table (entidadeProdutosFornecedores) and return
I'm using linq-to-sql for a query like this: public static List<MyModel> GetData(int TheUserID, DateTime
I have a table Users.. it has a column ShowData using linq-sql how can
I'm using Linq to SQL to query an Object from DB. I pass it
I'm using LINQ to SQL to obtain data from a set of database tables.
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,

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.