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

  • Home
  • SEARCH
  • 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 8392939
In Process

The Archive Base Latest Questions

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

I am using .Net Framework 4, mvc 3 and entity framework (latest stable) On

  • 0

I am using .Net Framework 4, mvc 3 and entity framework (latest stable)

On an index page I want to be able to filter each column. The filter values are retrieved for each column by LINQ using the distinct() method. I have not found a good way to reuse the method that retrieves these values for each column – they are basically equal, the only thing differentiating them is the column name used, so having one method instead of several would be great.

public List<string> GetLevels()
{
    return _db.Logs.Select(l => l.Level).Distinct().ToList();
}

public List<string> GetOrders()
{
    return _db.Logs.Select(l => l.Order).Distinct().ToList();
}

How can I inject the lambda expression with the column name I want to retrieve data from? Something like this:

public List<string> GetFilterValues(string columName)
{
    return _db.Logs.Select(l => l.columnName).Distinct.ToList();
}
  • 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:39:10+00:00Added an answer on June 9, 2026 at 7:39 pm

    You can write a function which takes an expression selecting the property you want:

    public List<T> GetLogProperties<T>(Expression<Func<Log, T>> selector)
    {
        return _db.Logs.Select(selector).Distinct().ToList();
    }
    
    public List<string> GetLevels()
    {
        return GetLogProperties(l => l.Level);
    }
    

    EDIT: If you want to create the projection from a string column, you will have to build the expression yourself:

    public List<string> GetFilterValues(string columnName)
    {
        var paramExpr = Expression.Parameter(typeof(Log), "l");
        var propExpr = Expression.Property(paramExpr, columnName);
        var lambdaExpr = Expression.Lambda<Func<Log, string>>(propExpr, paramExpr);
    
        return _db.Logs.Select(lambdaExpr).Distinct().ToList();
    }
    
    List<string> levels = GetFilterValues("Level");
    

    Note that this will throw an exception if the property with the given name is not a string property.

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

Sidebar

Related Questions

I am using ASP.NET MVC 3 with Entity Framework 4 using POCOs and want
Using ASP.NET MVC, Entity framework, jQuery. I want to give users the ability to
When using ASP.NET MVC plus Entity Framework, and trying to implement a generic repository
I am using the asp.net mvc with the Entity Framework. I have a list
i've started working with ASP.NET MVC and using Entity Framework, concretely with SQLite db,
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1 and
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1, however,
I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My
I am developing a ASP.NET MVC 3 application, i am using entity framework code

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.