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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:19:34+00:00 2026-06-08T21:19:34+00:00

I have a Enumerable<KeyValuePair<TKey, TValue>> . I want to create a bool TryGetValue(TKey, out

  • 0

I have a Enumerable<KeyValuePair<TKey, TValue>>. I want to create a bool TryGetValue(TKey, out TValue) extension method of it just like it is available in Dictionary<TKey, TValue>.

I tried

public static bool TryGetValue<TKey, TValue>
(this Enumerable<KeyValuePair<TKey, TValue>> mapping, TKey key, out TValue value)
{
    bool retVal = false;

    KeyValuePair<TKey, TValue> kvp;

    kvp = mapping.First(x => x.Key.Equals(key));

    if(kvp.Key == null && kvp.Value == null)
    {
        retVal = false;
        value = default(TValue);
    }
    else
    {
        retVal = true;
        value = kvp.Value;
    }

    return retval;
}

Is this correct way? If not please suggest one.

Note:

I cannot use a Dictionary because Keys are repeated. Moreover it will only return the first matching value?

What happens to the rest?

We can leave them. I am sending KeyValuePair created from a DataTable. I am creating that DataTable using order by columnname in its query.

  • 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-08T21:19:35+00:00Added an answer on June 8, 2026 at 9:19 pm

    Why not just use a simple foreach loop?


    Example:

    public static bool TryGetValue<TKey, TValue>
    (this KeyValuePair<TKey, TValue>[] mapping, TKey key, out TValue value)
    {
        foreach(var kvp in mapping)
            if (kvp.Key.Equals(key))
            {
                value = kvp.Value;
                return true;
            }
    
        value = default(TValue);
        return false;
    }
    

    Your implementation will throw an exception if the key doesn’t exists due to .First(), and FirstOrDefault() would be ugly since KeyValuePair is a struct and hence you can’t just compare it to null.


    Sidenote:

    Instead of extending KeyValuePair<TKey, TValue>[], you probably want to use IEnumerable<KeyValuePair<TKey, TValue>> instead to be more flexible.

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

Sidebar

Related Questions

Possible Duplicate: Extension method for Enumerable.Intersperse? I have a List<string> like this: foo, bar,
I have two objects @tracks (an enumerable) and @artist, and I'd like to create
I have an IEnumerable<KeyValuePair<string,string>> , from which I would like, ideally, an anonymous object
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the
In .NET, both array and list have Enumerable as ancestor, so a method that
Assume I have an enumerable object enum and now I want to get the
I want to be able to have an object extend Enumerable in Ruby to
I have several huge sorted enumerable sequences that I want to merge . Theses
I have a method that returns an IEnumerable that I want to be able
Lets say I have an enumerable source, that looks like this: IEnumerable<string> source =

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.