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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:26:29+00:00 2026-05-23T06:26:29+00:00

I have an IDictionary I need to select the first Foo where the Bar.Prop1

  • 0

I have an IDictionary

I need to select the first Foo where the Bar.Prop1 matches a string value.

public class Foo { }

public class Bar
{
    public String Prop1 { get; set; }
}

right now I have it like so…

foreach (var kvp in MyDictionary)
{
    if (kvp.Value.Prop1 == theString)
    {
        var key = kvp.Key;
        //Do something with it
        break;
    }
}

But that just doesn’t seem as clean as a LINQ Query is. ReSharper turned it into:

foreach (var kvp in MyDictionary.Where(kvp => kvp.Value.Prop1 == theString))
{
    var key = kvp.Key;
    //Do something with it
    //break; is unnecessary because I only get one kvp back anyways.
}

I only want the very first item that matches, because I don’t ever expect to get back more than one KVP. That goes against the business logic, so Unit Testing takes care of that.

  • 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-23T06:26:30+00:00Added an answer on May 23, 2026 at 6:26 am

    I only want the very first item that
    matches, because I don’t ever expect
    to get back more than one KVP. That
    goes against the business logic, so
    Unit Testing takes care of that.

    If this is the case, I would argue you need to use a stronger code guarantee of your intention, and that would be the Single (or SingleOrDefault) method. First is going to return the first object of arbitrarily many that match a given predicate. If many goes against your expectations and business rules, this seems to be an error. Treat it as such.

    var key = MyDictionary.Single(pair => pair.Value.Prop1 == someValue).Key;
    

    With Single, if there is more than one matching item in a sequence, it will result in an exception.

    SingleOrDefault allows for 0 or 1, but never more. If using this approach, you’d want to capture the result and compare to null before performing additional operations with it (firing methods, accessing properties, etc.).

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

Sidebar

Related Questions

Suppose I have these two objects: public class Object1 { string prop1; string prop2;
I need parse a select value in html file. I have this html file:
I have a simple Dictionary(of String, Object) that I need to iterate through and
I have a class derived from Dictionary. I need this class to simulate a
I have: IDictionary<string, IDictionary<string, IList<long>>> OldDic1; (just for illustration purposes, it is instantiated and
On my html page I have a dropdown list: <select name=somelist> <option value=234234234239393>Some Text</option>
I have two Generic lists having objects of class : class Subject { public
I need a Dictionary<string,object> which is produced from a DataRow. I currently have something
I have a class that I need to map that looks a bit like
I need to select stories from a NSArray of XML by matching a string

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.