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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:17:31+00:00 2026-06-06T14:17:31+00:00

For brevity’s sake in my code, i’d like to be able to do the

  • 0

For brevity’s sake in my code, i’d like to be able to do the following: having a collection, find the first element matching a lambda expression; if it exists, return the value of a property or function. If it doesn’t exist, return null.

Updated examples w. classes

Let’s have a collection of stuff

class Stuff
{
    public int Id { get; set; }
    public string Value { get; set; }
    public DateTime? ExecutionTime { get; set; }
}

What I am aiming for is a way to return nicely when calling this

var list = new Stuff[] { new Stuff() { Id = 1, Value = "label", ExecutionTime = DateTime.Now } };

// would return the value of ExecutionTime for the element in the list
var ExistingTime = list.FirstOrDefault(s => s.Value.Contains("ab")).ExecutionTime;

// would return null
var NotExistingTime = list.FirstOrDefault(s => s.Value.Contains("zzz")).ExecutionTime; 

Is it possible with some linq-syntax-fu or do I have to check explicitly for the return value before proceeding?

Original example w. strings

var stuff = {"I", "am", "many", "strings", "obviously"};

// would return "OBVIOUSLY"
var UpperValueOfAString = stuff.FirstOrDefault(s => s.contains("bvi")).ToUpper();

// would return null
var UpperValueOfAStringWannabe = stuff.FirstOrDefault(s => s.contains("unknown token")).ToUpper();

Comment:
I shouldn’t have used strings in my original example, since it slightly skews the question by centering it on the ToUpper method and the string class. Please consider the updated example

  • 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-06T14:17:32+00:00Added an answer on June 6, 2026 at 2:17 pm

    Why not just do:

    stuff.Where(s => s.contains("bvi"))
         .Select(s => s.ToUpper())
         .FirstOrDefault()
    

    If you have a “non-default default”, you can do:

    stuff.Where(s => s.contains("bvi"))
         .Select(s => s.ToUpper())
         .DefaultIfEmpty("Something Else")
         .First()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code (only relevant portions shown for sake of brevity - please
I have the following code if (msg.position == 0) //removed for brevity else if
I think I can explain myself without code, so for brevity's sake here we
Note: For brevity's sake, the following will not discern between randomness and pseudo-randomness. Also,
The following hunk of code (snipped for brevity) generates an xml doc, and spits
I am having trouble mapping the following database structure (shortened for brevity with just
I have a problem with the following scenario (code cut for brevity). Basically the
I have the following php code, which has been snipped for brevity. I am
In my controller I have the following (code omitted for brevity): public ActionResult Edit(string
I have the following code: li { a { /* style removed for brevity

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.