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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:44:08+00:00 2026-06-09T20:44:08+00:00

I created a Emp class class Emp { public Emp() { } public Emp(Int64

  • 0
  1. I created a Emp class

    class Emp
    {
        public Emp()
        {
        }
        public Emp(Int64 empId, string empName, double empSalary,int empDeptId)
        {
            this.EmpID = empId;
            this.EmpName = empName;
            this.EmpSalary = empSalary;
            this.EmpDeptId = empDeptId; 
        }
        public Int64 EmpID { get; set; }
        public string EmpName { get; set; }
        public double EmpSalary { get; set; }
        public Int32 EmpDeptId { get; set; }
    }
    
  2. I just Apply Group by command into EmpList then at return statement i got runtime exception that

An unhandled exception of type ‘System.InvalidCastException’ occurred in ConsoleApplication5.exe

Additional information:

Unable to cast object of type ‘WhereSelectEnumerableIterator2[System.Linq.IGrouping2[System.Int32,ConsoleApplication5.Emp],<>f__AnonymousType03[System.Int32,System.Double,System.Double]]' to type 'System.Collections.Generic.IEnumerable1[ConsoleApplication5.EmpGroup]’.

I also share the code which i have written.

public IEnumerable<EmpGroup> GroupByDeptId()
        {

            var numberGroups =from result in empList
                              group result by result.EmpDeptId into groupingData
                              select new 
                              {
                                  EmpDeptId = groupingData.Key,
                                  SalarySum = groupingData.Sum(p => p.EmpSalary),
                                  AverageSalary = groupingData.Average(p => p.EmpSalary) 
                              };

               return (IEnumerable<EmpGroup>)numberGroups;

        }

`3. I also Created a class for EmpGroup to handle the grouping data.

class EmpGroup
    {
        public Int32 EmpDeptId { get; set; }

        public double SalarySum { get; set; }

        public double AverageSalary { get; set; }
    }
  • 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-09T20:44:09+00:00Added an answer on June 9, 2026 at 8:44 pm

    You cannot cast from anonymous to your type, even if they have the same fields. You should edit your LINQ select to create a new EmpGroup:

    public IEnumerable<EmpGroup> GroupByDeptId()
            {
    
                var numberGroups =from result in empList
                                  group result by result.EmpDeptId into groupingData
                                  select new EmpGroup() // here
                                  {
                                      EmpDeptId = groupingData.Key,
                                      SalarySum = groupingData.Sum(p => p.EmpSalary),
                                      AverageSalary = groupingData.Average(p => p.EmpSalary) 
                                  };
    
                   return (IEnumerable<EmpGroup>)numberGroups;
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class FinalClass { public Employee emp{ get; set; } public Customer cust{ get;
I created a following class within a namespace Global namespace Global { public static
my table structure is CREATE TABLE [dbo].[Emp]( [ID] [int] NOT NULL, [EmpName] [varchar](50) NOT
I created entity from the tables . Here is my employee entity. Class Emp{
I created a JButton which has an image set as an icon representing the
I have an AlumniRecords table with 60+ columns. I created an AlumniSearchResults class that
In this StackOverFlow question , I created an employee database and provided a select-by-first
i created a class and when i create an employee object via form ,
sql has a Table called emp. emp(emp_id int IDENTITY primary key, EmployeeName varchar(50),.......) I
Created .NET WCF service, tested it - works. Generated schemas from Data and service

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.