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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:49:55+00:00 2026-05-23T00:49:55+00:00

Here is the full implementation I am considering: using System; using System.Collections.Generic; using System.Linq;

  • 0

Here is the full implementation I am considering:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Utils {
    public static class IDictionaryExt {
        public static IEnumerable<SelectListItem> ToSelectListItems<T, R>(this IDictionary<T, R> dic, T selectedKey) {
            return dic.Select(x => new SelectListItem() { Text = x.Value.ToString(), Value = x.Key.ToString(), Selected=(dynamic)x.Key == (dynamic)selectedKey });
        }
    }
}

Notice the equality check using the dynamic casts: (dynamic)x.Key == (dynamic)selectedKey. Is this the best way to check equality between selectedKey and x.Key here? Based on @Gabe’s comment in Operator '==' can't be applied to type T?, I believe it is: overload resolution is deferred to to runtime, but we do get “normal” overload resolution (i.e. considering ValueTypes and other Objects with == overloads versus Objects with default reference equality).

  • 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-23T00:49:56+00:00Added an answer on May 23, 2026 at 12:49 am

    The best way to handle this situation is to use EqualityComparer<T>.Default

    return dic.Select(x => new SelectListItem() { Text = x.Value.ToString(), Value = x.Key.ToString(), Selected= EqualityComparer<T>.Default.Equals(x.Key, selectedKey) });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Full implementation is here https://gist.github.com/1306491 I'm using struct with implicit casting to implicitly convert
Considering the full list of PDC videos published here what are, in your opinion,
Edit 2 Ok, here is the full example: public class FixedSizeLibraryViewModel : ViewModel {
I'm using SpongyCastle (full implementation of BouncyCastle's crypto functions for Android) and I have
Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
Ok, so here's the full description of the problem I'm having: I am trying
Someone posted a great little function here the other day that separated the full
Here is an implementation of a thread safe queue. The push and pop won't
Here's the code that raises the exception public Configuration GetConfiguration() { var persister =
Reaching out to the community here for a best practice/best implementation question. I am

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.