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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:34:54+00:00 2026-05-26T08:34:54+00:00

I am using a Data Entity from the db, and a Domain Service. I

  • 0

I am using a Data Entity from the db, and a Domain Service.

I am using .net’s generated code for the simple queries, like this

public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
    return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}

Now, I need to change the .Where section accordingly, so:

if (localeID > 0)
{
    ['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
    [IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
    Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}

and so on with other variables.

There are many possible combinations , which is why I was trying to use the overload for .Where(string, parameter[])

string q = string.Empty;
if (localeID > 0)
{
    q = "Locale_ID = " + localeID.ToString();
}

if (projectID > 0)
{
    q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);

However, this only gives an error, because then all the fieldnames are supposedly out-of-scope/non-existing. Even if use employees.[field_name] in the string, they “don’t exist”

Does anyone know of a way to use conditionals inside the .Where part of the query? Or how to create some var or object containing my query so I can just parse it?

  • 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-26T08:34:54+00:00Added an answer on May 26, 2026 at 8:34 am

    You can use

    IQueryable<Employee> emp =  ObjectContext.employees;
    if (localeID > 0)  
        emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();  
    
    if (projectID > 0)  
        emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();  
    

    This will concatenate the where clauses

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

Sidebar

Related Questions

How to add a FunctionImportMapping programmatically using classes from the System.Data.Entity.Design namespace? This blog
I have an ASP.NET Dynamic Data application (using Entity Framework) in which I have
in our current project we are using ADO.NET Entity Framework as data layer for
Last year I developed a data access service for our project using Entity Framework
I have created a repository that is returning data from my database using Entity
I'm using Entity Framework for creation of my Data Access Layer and I want
I am using DbProviderFactories in my data layer (based on Entity Framework) and am
I'm getting into using the Repository Pattern for data access with the Entity Framework
I have a very simple WPF application in which I am using data binding
I'm building a 4 layered ASP.Net web application. The layers are: Data Layer Entity

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.