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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:08:08+00:00 2026-06-02T07:08:08+00:00

I am struggling with converting a LINQ statement which returns an anonymous type to

  • 0

I am struggling with converting a LINQ statement which returns an anonymous type to an ObservableCollection with a custom class, I’m happy with the LINQ statement, and the class definition, the problem (I think) is to do with how I am implementing the IQueryable interface between my anonymous type and the class itself.

public class CatSummary : INotifyPropertyChanged
{
    private string _catName;
    public string CatName
    {
        get { return _catName; }
        set { if (_catName != value) { _catName = value; NotifyPropertyChanged("CatName"); } }
    }

    private string _catAmount;
    public string CatAmount
    {
        get { return _catAmount; }
        set { if (_catAmount != value) { _catAmount = value; NotifyPropertyChanged("CatAmount"); } }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify Silverlight that a property has changed.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            //MessageBox.Show("NotifyPropertyChanged: " + propertyName);

        }
    }

}

private void GetCategoryAmounts()
{
    var myOC = new ObservableCollection<CatSummary>();


    var myQuery = BoughtItemDB.BoughtItems
                        .GroupBy(item => item.ItemCategory)
                        .Select(g => new 
                        { 
                            _catName = g.Key, 
                            _catAmount = g.Sum(x => x.ItemAmount)
                        });

    foreach (var item in myQuery) myOC.Add(item);
}

The error I am getting is on the last line and is
"Argument 1: cannot convert from 'AnonymousType#1' to 'CatSummary'"

I’m relatively new to c# and need pointing in the right direction – if anyone has any tutorials on this sort of thing that would help as well.

  • 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-02T07:08:11+00:00Added an answer on June 2, 2026 at 7:08 am

    This is because the anonymous object you are creating has no type relationship with CatSummary. If you want to add those items into your ObservableCollection, you will need to construct a CatSummary like so:

    BoughtItemDB.BoughtItems.GroupBy(item => item.Category)
           .Select(x => new CatSummary
           {
               CatName = x.Key,
               CatAmount = x.Sum(amt => amt.ItemAmount)
           });
    

    This way, your query creates an IEnumerable<CatSummary> instead of IEnumerable<a'>. Unlike other languages and their duck typing, just because your newly created anonymous object has a CatName and CatAmount property does not mean it can stand in for the actual type.

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

Sidebar

Related Questions

I am struggling with converting multi-rows values which are belong to the same user
I am struggling with a problem, which I can't understand why it doesn't work.
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
Been struggling with this simple selector problem a couple of hours now and must
I am almost done converting a MySQL app to Linq2SQL but struggling to get
I'm struggling. I have query against my db that returns a single column of
Struggling with the following SQL problem. Assume a three dimensional table with entries (h,t,q)
I'm struggling converting a Response from http-conduit to an XML document via xml-conduit. The
I am struggling converting image to byte array using client side script. I have
We are converting over a existing system to DDD and are struggling to wrap

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.