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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:44:08+00:00 2026-06-09T19:44:08+00:00

I am seeking help on how to achieve this with LINQ in a type

  • 0

I am seeking help on how to achieve this with LINQ in a type safe way.

I need to perform search on “Performance” table with many columns. Based on the criteria specified for search I need to pick columns and perform search on those columns with given values.

private static IQueryable<Investment> PerformanceSearch(IQueryable<Investment> investments, **??? searchColumn**, double minValue, double maxValue)
{
  var entity = ExtendedEntities.Current;

  investments = from inv in entity.Investments 
                join performance in entity.Performances on inv.InvestmentID equals perfromance.InvestmentID
                where **performance.searchColumn** >= minValue && **performance.searchColumn** = maxValue
  return investments;
}

Now I am seeking your help on:

  1. How to pass column “searchColumn” to this method in a type safe way? I was thinking of creating a dictionary object to accommodate some way to maintain column names from entity framework. But not sure how to achieve this.

  2. How to perform LINQ query using the columnName passed and applying where clause.

I cannot use If Else or Switch case as below is a list of possible search columns…

/*
 * Search Columns can be:
 *      "Return1Month", "Return2Months", "Return3Months", ... almost 10 more and
 *      "Risk1Month", "Risk2Months", "Risk3Months", ... almost 10 more and
 *      "TrackingError1Month", "TrackingError2Months", "TrackingError3Months", ... almost 10 more and
 *      2 more similar set of columns ... 
 */

I spent time on Stackoverflow, Microsoft and other blogs and considered using Dynamic LINQ but it’s not type safe. It seems that it is achievable using expressions but couldn’t get that working.

Any advice is appreciated.

EDIT –

Another item to mention – all search columns are present in “performance” table.

  • 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-09T19:44:09+00:00Added an answer on June 9, 2026 at 7:44 pm

    Hands down, LINQ expressions are the best way to dynamically build LINQ queries in a strongly typed manner. You are absolutely right to discard the Dynamic LINQ Library! LINQ Expressions are challenging to grasp at first, but I promise you that the end pay off is well worth the effort.

    Here is an example that uses LINQ expressions to accomplish what you want. You’ll notice it doesn’t include any string column names, switch statements, helper classes, or enums. You will need to import the System.Linq.Expressions namespace for this to work:

    EDIT: The example now includes filtering by a column on one joined table, while selecting an element from another. I also removed the investments parameter from the method, as you don’t actually need to pass that in. You are just accessing the EF tables directly in the method (which I substitute for _performance and _investments).

        public static IQueryable<Investment> PerformanceSearch(Expression<Func<Performance, double>> searchColumn, double minValue, double maxValue) {
    
            // LINQ Expression that represents the column passed in searchColumn
            // x.Return1Month
            MemberExpression columnExpression = searchColumn.Body as MemberExpression;
    
            // LINQ Expression to represent the parameter of the lambda you pass in
            // x
            ParameterExpression parameterExpression = (ParameterExpression)columnExpression.Expression;
    
            // Expressions to represent min and max values
            Expression minValueExpression = Expression.Constant(minValue);
            Expression maxValueExpression = Expression.Constant(maxValue);
    
            // Expressions to represent the boolean operators
            // x.Return1Month >= minValue
            Expression minComparisonExpression = Expression.GreaterThanOrEqual(columnExpression, minValueExpression);
    
            // x.Return1Month <= maxValue
            Expression maxComparisonExpression = Expression.LessThanOrEqual(columnExpression, maxValueExpression);
    
            // (x.Return1Month >= minValue) && (x.Return1Month <= maxValue)
            Expression filterExpression = Expression.AndAlso(minComparisonExpression, maxComparisonExpression);
    
            // x => (x.Return1Month >= minValue) && (x.Return1Month <= maxValue)
            Expression<Func<Performance, bool>> filterLambdaExpression = Expression.Lambda<Func<Performance, bool>>(filterExpression, parameterExpression);
    
            // use the completed expression to filter your collection
            // This requires that your collection is an IQueryable.
            // I believe that EF tables are already IQueryable, so you can probably
            // drop the .AsQueryable calls and it will still work fine.
            var query = (from i in _investments
                         join p in _performance.AsQueryable().Where(filterLambdaExpression)
                           on i.InvestmentId equals p.InvestmentId
                         select i);
    
            return query.AsQueryable();
    
        } 
    

    You would call PerformanceSearch this way, using this simple console app as an example:

        private static IList<Investment> _investments;
        private static IList<Performance> _performance;
    
        static void Main(string[] args) {
    
            // Simulate your two Entity Framework tables
            BuildMockDataset();
    
            // Return1Month is on Performance, but I return IQueryable<Investment>;
            var results = PerformanceSearch(x => x.Return1Month, 300, 1000);
    
        }
    

    This example is generic enough to allow you to pass a double property from Performance as searchColumn, specifying min and max values as double.

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

Sidebar

Related Questions

First of all this is not a question seeking Programming help. I am doing
This isn't much of a coding question as opposed to seeking help on going
i am seeking help on updating one column in a table with results from
I am not familiar with this website but I am desperately seeking help with
Seeking help to design a layout as shown here: http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185MTd0eGdyZmc&hl=en The major challenge I
Seeking your help to get off .epub(s) sea in iPhone world... I am trying
I am newbie in Ruby.. seeking some help.. I have a code DB =
Seeking some help to determine what the heck is the issue as to why
i am seeking help on a query which shows movement of a stock item
Seeking help from Nginx experts here. I want to block people accessing CSS or

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.