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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:04:21+00:00 2026-05-29T17:04:21+00:00

I have a list of objects similar to the one below ( MyData class)

  • 0

I have a list of objects similar to the one below (MyData class) and I would use a LINQ query to get only those that meet a constraint on SecondDate property, and finally sort the items that meet this constraint on the basis of all properties.

public class MyData
{
    public Nullable<DateTime> FirstDate {get; set;}
    public DateTime SecondDate {get; set;}
    public TimeSpan SimpleInterval {get; set;}
    public double SimpleValue {get; set;}
    ...
}

I wrote the following query, but I do not know how to handle the Nullable <DateTime> FirstDate property. An item which has this property set to null should be sorted lower than another item which has identical values ​​of the fields except this property set to a not null value.

// public static void main
double threshold = ... // some initialization
DateTime now = DateTime.Now;
List<MyData> list = new List<MyData>();
...
var items = from item in list
            where ((now - item.SecondDate).TotalSeconds < threshold))
            orderby item.FirstDate descending
            orderby item.SecondDate descending
            orderby item.SimpleInterval descending
            orderby item.SimpleValue descending
            select item
  • 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-05-29T17:04:26+00:00Added an answer on May 29, 2026 at 5:04 pm

    You can use the HasValue property, for example:

    orderby item.FirstDate.HasValue descending,
            item.FirstDate.GetValueOrDefault() descending
    

    Note that using multiple orderby clauses in a query expression is almost certainly not what you want to do – your query should probably be:

    var items = from item in list
                where ((now - item.SecondDate).TotalSeconds < threshold))
                orderby item.FirstDate.HasValue descending,
                        item.FirstDate.GetValueOrDefault() descending,
                        item.SecondDate descending,
                        item.SimpleInterval descending,
                        item.SimpleValue descending
                select item;
    

    Note that this is a good general-purpose solution, because it doesn’t rely on the default value being lower / higher than anything in your data. default(DateTime) is actually equal to DateTime.MinValue in reality, but IMO it’s nice not to rely on that, but separately say “I first want to bucket by null vs non-null, then the value”. It also means that the non-null data is sorted before the null data even if there are non-null DateTime.MinValue values.

    In your current query, it’s effectively performing a new (stable) sort on each field, rather than only using it as a tie-breaker. If your previous query was giving you what you wanted, then you should reverse the order of the orderings (as it were):

    var items = from item in list
                where ((now - item.SecondDate).TotalSeconds < threshold))
                orderby item.SimpleValue descending,
                        item.SimpleInterval descending,
                        item.SecondDate descending,
                        item.FirstDate.HasValue descending,
                        item.FirstDate.GetValueOrDefault() descending
                select item;
    

    Consecutive orderby clauses are almost always a mistake, basically.

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

Sidebar

Related Questions

How do I use LINQ to update objects in one list from objects in
I have a list of objects which I built with a class, and one
I have a list of custom objects List and I would like to update
I have a list of objects called Activity: class Activity { public Date activityDate;
Ok this is a tricky one. I have a list of Sets. I would
I have a model similar to this one: class MyModel(models.Model): name = models.CharField(max_length =
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have 2 List objects: List<int> lst1 = new List<int>(); List<int> lst2 = new
I have List where myObj has it own list of objects called example mySubObj.
I have a list of objects that have two int properties. The list is

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.