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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:46:37+00:00 2026-05-22T22:46:37+00:00

I have an object that has a KeyValuePair type property. I would like to

  • 0

I have an object that has a KeyValuePair type property.

I would like to read some data from a database and store results in this KeyValuePair type field.

myObject.KeyValuePairs = ctx.ExecuteQuery<KeyValuePair<String, int>>
                        ("Select " +
                            "[" + q.Name + "] As [Key]" +
                            ", Count([" + q.Name + "]) As [Value] From SomeTable" + 
                            " Group By [" + q.Name + "]").ToList(); 

myObject.KeyValuePairs is a List<KeyValuePair<String, int>>

When I attempt to read the records I get the following exception:

The type ‘System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]’ must declare a default (parameterless) constructor in order to be constructed during mapping.

I have a default constructor in a class, but this is not fixing the problem. It looks as if It doesn’t know how to construct a KeyValuePair object. Doesn’t it have a default constructor? Confused..

Thank you

  • 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-22T22:46:37+00:00Added an answer on May 22, 2026 at 10:46 pm

    It does have a public parameterless constructor because KeyValuePair<TKey, TValue> is a struct and all struct have implicit public parameterless constructor.

    The issue is that EF can’t find it by reflection because reflection does not return the default constructor for struct.

    This is why EF is reporting that it can’t find it (it can’t find it by reflection).

    Additionally, even if you could use the default constructor by reflection, KeyValuePair<TKey, TValue> is immutable and so you can’t set the Key and Value after construction.

    To solve your problem, define a custom class

    class NameCount {
        public string Name { get; set; }
        public int Count { get; set; }
    }
    

    and change your query to return the Key as Name and Value as Count. I assume that you can come up with a better name for your custom class.

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

Sidebar

Related Questions

I have an object that has just one member with a property of type
I have an object that has some properties and methods, like so: {name: FirstName,
Hey. I have an object that has a string property called BackgroundColor. This string
I have an Asset object that has a property AssignedSoftware, which is a collection.
I have an object which has a property that contains an IEnumerable of objects.
We have a .Net object that has a static delegate (a data access strategy)
I have an xts object that has intraday data: head(stocks[,1]) SMH.close 2009-01-02 09:31:00 17.66
I have defined an object that has several attribute.. class thing(object): def __init__(self, type,
I would like to have a MovieClip that has a bit of depth. I
I have an object that has a char property: public class Product { public

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.