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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:59:25+00:00 2026-06-17T01:59:25+00:00

I frequently need to limit SELECTs by fields like publishStart , publishEnd , active

  • 0

I frequently need to limit SELECTs by fields like publishStart, publishEnd, active

I have these fields in several different tables. So only rows should be selected, that are

a: active == true;
b: publishStart < now;
c: publishEnd > now;

So, for example:

db.myTable.SingleOrDefault(a => (a.ID == _theID 
          //now the active and start-end part:            
                      && ((a.publishEnd > DateTime.Now) || (a.publishEnd == null))
                      && ((a.publishStart <= DateTime.Now) || (a.publishStart == null))
                      && a.active == true));

This is a bit lengthy, so I wonder if it is possible to create a (extension?)-method like:

db.myTable.SingleOrDefault(a => (a.ID == _theID).isActive()

where the isActive() provides the 3 lines of the above snippet.

How could I do this?
Is there a better way to clean up code?

  • 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-17T01:59:26+00:00Added an answer on June 17, 2026 at 1:59 am

    To define an extension you need a static class. You can put this in whatever namespace you like, just remember to include it in your usings.

    public static class Extensions
    {
        public static IQueryable<T> Active<T>(this IQueryable<T> source)
            where T : YourEntityType
        {
            return source.Where(a => ((a.publishEnd > DateTime.Now) || (a.publishEnd == null))
                              && ((a.publishStart <= DateTime.Now) || (a.publishStart == null))
                              && a.active == true);
        }
    }
    

    Notice YourEntityType in there. This is used to ensure the method is aware of the existence of publishStart, publishEnd, and active. This should be either the class that implements these fields or a contract (interface) that defines them.

    You would then call this like so:

    var item = db.myTable.Active().SingleOrDefault(...);
    

    More on extension methods here: http://msdn.microsoft.com/en-us/library/bb383977.aspx


    As there are lots of comments popping up all over the place, I’m going to add here a brief explanation of the interface solution…

    It’s unclear in the question whether or not there is a common implementation for the three filtered fields or an interface to define them. If not, for the above to work, you will not either:

    1. A base class implementing these fields. In this scenario you would replace YourEntityType with YourBaseEntityType.
    2. An interface to define the fields. In this scenario you would need to have your classes implement the fields. If the classes are auto generated (e.g. entity framework model/db first) then you can implement partial classes, having them implement the interface. In this case you would replace YourEntityType with IYourContract.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file called error.log on my server that I need to frequently
I need to merge between dev and master frequently. I also have a commit
I have a table into which new data is frequently inserted. I need to
I have a private LinkedList in a Java class & will frequently need to
We have following problem. Developers frequently need to make small changes to our web
I have a 2d rectangular grid and I need to apply different functions to
I have a need to frequently run an applescript on my web server. As
I have an issue where I need to ONLY get a few things from
I frequently need to run the command Rails.cache.clear on Heroku and the only way
In my models I frequently have has_many associations. I also frequently need to display

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.