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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:11:04+00:00 2026-06-09T00:11:04+00:00

I am using LINQ to Entities and querying for entries within a certain date

  • 0

I am using LINQ to Entities and querying for entries within a certain date time period, held in model object which has the properties DateFrom and DateTo. To do this I can use the following to create a sequence

var allActiveLogs = this.repository.RoutePerformanceLogs
         .Where(log => log.Date >= model.DateFrom.Value && 
                log.Date <= model.DateTo.Value)

If I want to abstract this out for reuse I can create the following expression (as long model is in scope).

Expression<Func<RoutePerformanceLog, bool>> logWithinDateBounds = log => 
        log.Date >= model.DateFrom.Value && log.Date <= model.DateTo.Value;

and then later call

var allActiveLogs = this.repository.RoutePerformanceLogs.Where(logWithinDateBounds)

What I would like to do is abstract this expression further, to code where model is not in scope, possibly using an expression of signature

Expression<Func<RoutePerformanceLog, DateTime?, DateTime?, bool>> logWithinDateBounds

However, this won’t work within a Where method, as the where method requires a Func<T, boolean> or an Expression<Func<T, boolean>>.

Is it possible to have a reusable expression, that takes more than one parameter and can be used to filter an IQueryable collection (preferably using the query provider to do the filtering rather than filtering objects in memory).

  • 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-09T00:11:06+00:00Added an answer on June 9, 2026 at 12:11 am

    I hope this can help. It is a quite functional programming approach. You can create a function (or an Expression) returning a function and using that function for the Where.

    Something like the following:

    Func<int, int, Func<int,bool>> func = (x, y) => z=> x + y > z;
    var list = new List<int> { 1, 2, 3, 4, 5, 6 };
    
    Console.WriteLine("How many greater than 2+1? {0}", 
                      list.Where(func(1, 2)).Count());
    Console.WriteLine("How many greater than 3+1? {0}", 
                      list.Where(func(3, 1)).Count());
    Console.WriteLine("How many greater than 2+3? {0}", 
                      list.Where(func(2, 3)).Count());
    Console.ReadKey();
    

    In your case you need:

    Func<DateTime, DateTime, Expression<Func<RoutePerformanceLog, bool>>> logWithinDateBounds =         
        (dateFrom, dateTo) => 
           log => log.Date >= dateFrom && log.Date <= dateTo;
    
    • 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.
I'm using LINQ to Entities on a database which structure is not known in
We have a website which is using linq to entities, we found that it's
Using linq to entities i am connecting to a database, the database has tables
I'm using Linq to entities applying a Table per Type approach. This has been
For a project using LINQ to Entities 4.0, I have a setting in which
I am using Linq-To-Entities to do a query which is returning only 947 rows
I'm using LINQ to Entities. I have a table called Student; it has ID
I'm using linq to entities (C# Winforms) and my database has the following structure:
I'm using linq to entities, and my entity model is sitting on top of

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.