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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:36:02+00:00 2026-06-05T13:36:02+00:00

//I need to deifne productQuery here if (test == true) { var productQuery =

  • 0
//I need to deifne productQuery here
    if (test == true)
    {
      var productQuery = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
    }
    else
    {
      var productQuery = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
    }

How can I define the productQuery variable?

Thank you!

[EDIT]

dynamic productType;

if (test == true)
    {
      productType = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
    }
    else
    {
      productType = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
    }

var productQuery = productType as IQueryable<ProductIn>;
if (productQuery == null)
{
     productQuery = productType as IQueryable<ProductIn>;
}

I do this way, is it right way?

  • 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-05T13:36:04+00:00Added an answer on June 5, 2026 at 1:36 pm

    You could declare it as a dynamic type, if you’re ok with deferring type resolution until runtime.

    dynamic productQuery;
    if (test == true)
    {
        productQuery = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
    }
    else
    {
        productQuery = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
    }
    

    Your other choice is to declare it as an object type, and then cast it back to the other type as needed.

    object productQuery;
    
    if (test == true)
    {
        productQuery = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
    }
    else
    {
        productQuery = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
    }
    
    // ... more logic ...
    var unboxed = productQuery as IQueryable<ProductIn>;
    if (unboxed != null) {
        unboxed.Where( ... and away you go with Linq ...);
    }
    

    Update after op’s edit

    Let’s say you have a dynamic type productQuery. To use Linq on it, you need to define the types of the delegate. Let’s say that types ProductIn and ProductOut each have a string-type property ProductNo. Then you could write your query like this, again making use of dynamic.

    productQuery.Where(new Func<dynamic,bool>(item => item.productNo));
    

    However … I think you could make your life a lot easier by changing your whole approach. You’re clearly working against a common interface for ProductIn and ProductOut, so why not define that explicitly?

    public interface IProduct
    {
        public string ProductNo { get; set; }
    }
    
    public class ProductIn : IProduct { ... }
    public class ProductOut : IProduct { ... }
    

    Now your code becomes a lot simpler. Write it like this:

    IQueryable<IProduct> productQuery;
    
    if (test == true)
    {
        productQuery = ordersRepository.ProductIn; //it returns IQueryable<ProductIn> type
    }
    else
    {
        productQuery = ordersRepository.ProductOut; //it returns IQueryable<ProductOut> type
    }
    
    string myResult = productQuery.Where(item => item.productNo == productNo).FirstOrDefault(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to define mutually-dependent vars. By this I mean that one var contains
I need to define a one-to-one relationship, and can't seem to find the proper
I need to define a matrix of int as a type. A column or
I need to define a system-dependent integer type, in order to be compatible with
I need to define some path to files with macros. How can I use
I need to define a rake task that can handle any amount of arguments,
Here's what I was thinking of this morning: I need to define a lot
I need to define a pattern so that the input string can only contain
I need to define some common piece of code that can be invoked from
I need to define a template struct such that: element<T>::type is of type: T::element_type

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.