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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:32:35+00:00 2026-06-07T02:32:35+00:00

I need a help in LINQ. here is my scenario. I’ve two tables for

  • 0

I need a help in LINQ. here is my scenario. I’ve two tables for storing Employee details like name, ID, Salary, Primary_Emp and Year. User can either select Name or ID from DDL and Pass the input value to SP. SQL Server will return the data based on the input given.

tbl_Employee
EmpID   EmpName     Salary  Primary_Emp
1       xxx         10000   Yes
2       yyy         20000   Yes
3       zzz         30000   Yes

tbl_Year
EmpID   [Year]
1       2010
2       2011
3       2011

Here is my SQLQuery. I wanted to apply the same conditions in LINQ.
NOTE:- User has two option to select EmpID or EmpName from DDL and input the value in textbox (Free text)

Input Parameters:
Name varchar(100)
EmpID varchar(100)

select distinct e.EmpID from 
tbl_employee e
inner join 
tbl_Year y
on e.EmpID = y.EmpID
where E.Primary_Emp = 'Yes'
**AND e.EmpName = (SELECT CASE WHEN @Key = 'Name' THEN @Value ELSE e.Empname END)
    AND e.EmpID = (SELECT CASE WHEN @Key = 'EmpID' THEN @Value ELSE e.EmpID END)**

Question: how to form the dynamic where clause in linq. Here the problem is LHS is also dynamic. If the user pass EmpID, it should not consider the Name and Vice versa.

JOIN in linq is mandatory!

  • 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-06-07T02:32:36+00:00Added an answer on June 7, 2026 at 2:32 am

    If it’s just the separate and filter expressions you want to apply conditionally, you could do the following:

    var query = from e in dbcontext.tbl_employee 
                join y in tbl_Year 
                on e.EmpID equals y.EmpID 
                where e.Primary_Emp = "Yes"
                select e;
    
    if (key == "EmpName")
        query = from e in query where e.EmpName = value select e;
    else if (key == "EmpID")
        query = from e in query where e.EmpID = value select e;
    
    var result = (from e in query
                  select e.EmpID).Distinct();
    

    You can simply keep stacking where clauses conditionally this way.

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

Sidebar

Related Questions

I need help with LINQ syntax or methodology, not sure which. Here's my issue:
I need help with a specific LINQ query (I still suck at em'!) Background
I need help in turning this linq expression into a linq statment. SampleData.Publishers and
I need some help with a LINQ query in VB.Net, please. I have this
We are using Linq-to-SQL with stored Procedures we need help on one of our
I need some help adding conditions to a linq query. Take this small example
I need immediate help. ): and i know little english. ASP.NET MVC 3 Linq
Need help with a query that I wrote: I have three tables Company id
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
I've reached the end of my Linq rope. Need your help! Heres my table

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.