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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:47:00+00:00 2026-05-18T02:47:00+00:00

I am using conditional where clauses in linq to entities as below Dim q

  • 0

I am using conditional where clauses in linq to entities as below

    Dim q = (From x In ctx.Product)

    If mySearchField = SearchField.ProductId Then
       q = q.Where(Function(y) y.ProductId = mySearchTerm)
    ElseIf s.SearchField = SearchField.ProductCode Then
       q = q.Where(Function(y) y.ProductCode = mySearchTerm)
    ElseIf s.SearchField = SearchField.ProductName Then
       q = q.Where(Function(y) y.ProductName = mySearchTerm)
    End If

Dim productIds As List(Of Integer) = (From x In q Select x.ProductId).ToList

However when i view the generated sql via

Debug.Print(DirectCast(q, System.Data.Objects.ObjectQuery(Of Product)).ToTraceString)

The generated sql shows that it selects all three columns of the product class when all I need to return is ProductId.

SELECT 
[Extent1].[ProductId] AS [ProductId], 
[Extent1].[ProductCode] AS [ProductCode], 
[Extent1].[ProductName] AS [ProductName]
FROM (SELECT 
      [Product].[ProductId] AS [ProductId], 
      [Product].[ProductCode] AS [ProductCode], 
      [Product].[ProductName] AS [ProductName]
      FROM [dbo].[Product] AS [Product]) AS [Extent1]

Is there anyway of forcing EF to only select the columns I specify but keep the conditional where clauses on any property I need on the Product class?

  • 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-18T02:47:01+00:00Added an answer on May 18, 2026 at 2:47 am

    You can create your Where clause dynamically as an Expression, and then apply the expression at the same time as you select the data from the Linq Context.

    Dim MyWhereExpr As Expression(Of Func(Of Product, Bool))
    MyWhereExpr = Function(y) True
    
    If ... Then
        MyWhereExpr = Function(y) y.ProductId = mySearchTerm
    ElseIf ...
    
    Dim productIds As List(Of Integer) = _
        ctx.Product.Where(MyWhereExpr).Select(Function(y) y.ProductId).ToList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using linq to entities(EF). I have a constructor which takes 4 string parameters.
Using Linq to Entities, I have an IQueryable<T> and want to add another WHERE
I have this project set up with EF4 and I'm using LINQ to Entities
I am trying to bring some data from Dynamics CRM 2011 using LINQ. The
I am reading data from XML file using from..select linq query, the data is
I'm trying to call WordPress's 'the_time' using a conditional statement that checks the category.
I'm trying to create an conditional Rewrite Rule using an .htaccess file and Apache.
I am using ExpressionEngine 2.2.1 and am trying to create a conditional that will
I am using Oracle 11g client, with ODP.NET. I am trying to add conditional
I was just reading a recent question on using conditionals in Linq and it

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.