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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:35:07+00:00 2026-06-13T04:35:07+00:00

As I understand it, in Linq the method FirstOrDefault() can return a Default value

  • 0

As I understand it, in Linq the method FirstOrDefault() can return a Default value of something other than null. What I haven’t worked out is what kind of things other than null can be returned by this (and similar) method when there are no items in the query result. Is there any particular way that this can be set up so that if there is no value for a particular query some predefined value is returned as the default value?

  • 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-13T04:35:09+00:00Added an answer on June 13, 2026 at 4:35 am

    General case, not just for value types:

    static class ExtensionsThatWillAppearOnEverything
    {
        public static T IfDefaultGiveMe<T>(this T value, T alternate)
        {
            if (value.Equals(default(T))) return alternate;
            return value;
        }
    }
    
    var result = query.FirstOrDefault().IfDefaultGiveMe(otherDefaultValue);
    

    Again, this can’t really tell if there was anything in your sequence, or if the first value was the default.

    If you care about this, you could do something like

    static class ExtensionsThatWillAppearOnIEnumerables
    {
        public static T FirstOr<T>(this IEnumerable<T> source, T alternate)
        {
            foreach(T t in source)
                return t;
            return alternate;
        }
    }
    

    and use as

    var result = query.FirstOr(otherDefaultValue);
    

    although as Mr. Steak points out this could be done just as well by .DefaultIfEmpty(...).First().

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

Sidebar

Related Questions

As far as I can understand, the linq method FirstOrDefault() returns null if a
It seems that NHibernate Linq doesn't understand grouped .Where method conditions. I got the
I can't currently understand why my update method in my repository for the style
I understand I can map a delete stored procedure to the delete method for
The Linq to SQL Where extension method takes, as a parameter, something that looks
As far as I can understand, when I new up a Linq to SQL
I love the Linq syntax and its power, but sometimes I just can't understand
i need help to understand linq join. i found out a few topics related
I am trying to understand dynamic linq and expression trees. Very basically trying to
Every LINQ blog I found there seemed around 2 years old, I understand the

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.