Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hy i am not very much familiar to asp.net . Explain briefly why em encountering this error.

here is my code

C#
public List<Department> getDeptNames(){
using (
RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities())
{
var obj = context.Departments.ToList();
List<Department> dic = (from p in context.Departments
select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);
return obj;

}}
Posted
Comments
Tejas Vaishnav 5-Mar-15 1:28am    
can you share your Department class here, so i can help you with your problem.
Arif H Shigri 5-Mar-15 4:05am    
public partial class Department
{
public Department()
{
this.Employees = new HashSet<employee>();
}

public int AddressId { get; set; }
public int ZoneId { get; set; }
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
public string PhoneNo { get; set; }

public virtual Address Address { get; set; }
public virtual Zone Zone { get; set; }
public virtual ICollection<employee> Employees { get; set; }
}
Arif H Shigri 5-Mar-15 4:14am    
The entity or complex type 'RailwayElectricBillingDBModel.Department' cannot be constructed in a LINQ to Entities query.

suppose your Department class is something

C#
public class department
   {
       public int DepartmentId { get; set; }
       public string DepartmentName  { get; set; }
   }



then you need to write your query like this...

C#
public List<Department> getDeptNames()
    {
        using (RailwayElectricBillingDBEntities context = new RailwayElectricBillingDBEntities())
        {
            List<Department> dic = (from p in context.Departments
                                    select new Department
                                        {
                                            DepartmentId = p.DepartmentId,
                                            DepartmentName = p.DepartmentName
                                        }
                                    ).ToList();
            return obj;
        }
    }
 
Share this answer
 
Comments
Arif H Shigri 5-Mar-15 2:12am    
thanks bro.
Arif H Shigri 5-Mar-15 2:29am    
@Tehjas cannot initialize department with collection initializer because it does not implements system.collection.IEnumberable..
Should I implement the i enumerable interface ,,???
Arif H Shigri 5-Mar-15 3:01am    
#Tehjas Department is not implementing Ienumerable interface ..
(from p in context.Departments
select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);


You are selecting an Anonymous type, hence will not be able to cast it to List<department></department>


Try:

C#
(from p in context.Departments
select p).GroupBy( o=> o.DepartmentName).ToList();
 
Share this answer
 
Try this

C#
List<department> dic =(List<department>) (from p in context.Departments
select new { p.DepartmentId, p.DepartmentName }).GroupBy( o=> o.DepartmentName);</department></department>
 
Share this answer
 
Comments
Arif H Shigri 5-Mar-15 0:59am    
Now Getting This Error:
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Linq.IGrouping`2[System.String,<>f__AnonymousType0`2[System.Int32,System.String]]]' to type 'System.Collections.Generic.List`1[DataLayer.Department]'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900