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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:42:06+00:00 2026-06-11T05:42:06+00:00

I have defined a super class Validity which defines a time span (ValidFrom /

  • 0

I have defined a super class “Validity” which defines a time span (ValidFrom / ValidTo) in which an object is “valid”. It also defines a function that returns true for a given timestamp, iff (=if and only if) a (derived) object is valid at this time.

public class Validity
{
    public int ValidityID { get; set; }

    public DateTime? ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }

    bool isValidAt(DateTime time)
    {
        return (ValidFrom == null || ValidFrom >= time)
            && (ValidTo == null || ValidTo < time);
    }
}

Now I would like to write some function that checks isValidAt within a LINQ query. I guess this is possible via IQueriable, but I didn’t find out how…
The following code snipped is what I want to have “working” in some way (especially the where n.isValidAt(t)). So, how can this be achieved?

public class Node : Validity {
    public int NodeID { get; set; }

    public static getFirstNode(DateTime t)
    {
        MyContext db = new MyContext();
        var items = from n in db.Nodes
                     where n.isValidAt(t)
                     orderby n.NodeID descending
                     select n;
        return items.FirstOrDefault<Node>();
    }
}

— WORKING SOLUTION —

I needed to adapt the solution of Zaid Masud a bit, to get it working. Note that I had to remove the this in the parameter list (now the method definition is public static IQueryable<T> isValidAt<T>(IQueryable<T> query, DateTime time) where T : Validity). Here is the source code:

public class Validity
{
    public int ValidityID { get; set; }

    public DateTime? ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }

    public static IQueryable<T> isValidAt<T>(IQueryable<T> query, DateTime time) where T : Validity
    {
        return query.Where<T>(c => (c.ValidFrom == null || c.ValidFrom >= time)
            && (c.ValidTo == null || c.ValidTo < time));
    }
}
  • 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-11T05:42:08+00:00Added an answer on June 11, 2026 at 5:42 am

    You need to declare your bool isValidAt(DateTime time) method as protected so the derived class can access it:

    protected bool IsValidAt(DateTime time)
    

    However, after you get this compiling, I doubt that your LINQ to SQL provider will be able to translate the query to SQL. You will probably need to embed the logic inside your LINQ query and write something like:

    var items = from n in db.Nodes
                where (n.ValidFrom == null || n.ValidFrom >= t) && (n.ValidTo == null || n.ValidTo < t)
                orderby n.NodeID descending
                select n;
    

    This will work, but a better alternative is to create the following kind of extension method:

    public static class ValidityExtensions
    {
        public static IQueryable<T> Valid<T>(this IQueryable<T> validities, DateTime time) where T : Validity
        {
            return validities.Where(v => (v.ValidFrom == null || ValidFrom >= time) && (v.ValidTo == null || v.ValidTo < time));
        }
    }
    

    Now you can use this as follows:

    var items = from n in db.Nodes.Valid(time)
                orderby n.NodeID descending
                select n;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done a few projects lately using a Database Object super class which
I have a super pom defined in which I specify a "resources" directory for
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have this super class which extends from another class public abstract class AbstractDOEMessageFinderAction
I have an AbstractEntity class as superclass for all my entites that defines an
I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
I have defined the following two functions test <- function(t) { return( (0.5*eta^2/theta)*(1-exp(-2*theta*t)) )
I have an app which is a UITabBarController, I have defined two subviews Both
I have a abstract super class called RealAlgebraicNumber and two inherited classes called IntervalRepresentation
If I have this class defined, how do I access the someObject property in

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.