Casting in LINQ
-
Hi guys I have a problem in LINQ. My code looks like this:
var EmpDetails = from emp in mymodel.Employees
select new
{
Id = emp.Id,
EmpName = emp.Name
};When I assign the result to a list of employees like this:
List lstEmployee = EmpDetails;
I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.
-
Hi guys I have a problem in LINQ. My code looks like this:
var EmpDetails = from emp in mymodel.Employees
select new
{
Id = emp.Id,
EmpName = emp.Name
};When I assign the result to a list of employees like this:
List lstEmployee = EmpDetails;
I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.
You are trying to cast an anonymous type to a list of defined types. Why not select all the employee data (so you have a defined type).
var EmpDetails = from emp in myModel.Employees
select emp;List<\employees\> list = empDetails.ToList();
assuming you entity type is employees. If this isn't what you are looking for give me a shout :)
At university studying Software Engineering - if i say this line to girls i find they won't talk to me Dan
-
Hi guys I have a problem in LINQ. My code looks like this:
var EmpDetails = from emp in mymodel.Employees
select new
{
Id = emp.Id,
EmpName = emp.Name
};When I assign the result to a list of employees like this:
List lstEmployee = EmpDetails;
I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.
Or maybe
var lstEmployee = EmpDetails.ToList();
Eslam Afifi
-
Hi guys I have a problem in LINQ. My code looks like this:
var EmpDetails = from emp in mymodel.Employees
select new
{
Id = emp.Id,
EmpName = emp.Name
};When I assign the result to a list of employees like this:
List lstEmployee = EmpDetails;
I'm getting an error saying "Cannot convert implicitly from System.Collections.Generic.List to System.Collections.IEnumerable....". How could I avoid this error? Any help pls... Thanks Muthu.
List is a generic type. You can't just have List, you have to have to specify a type for T:
List<T>
. You don't actually have a known type because you created anonymous types in your query, so you had to use avar
type variable to hold the results. If you want to be able to create aList<T>
, you need a concrete type:class EmpDetail
{
public int Id { get; set; }
public string EmpName { get; set; }
}// ...
IEnumerable<EmpDetail> empDetails = from emp in mymodel.Employees select new EmpDetail { Id = emp.Id, EmpName = emp.Name };
List<EmpDetail> empDetailsList = empDetails.ToList(); -
List is a generic type. You can't just have List, you have to have to specify a type for T:
List<T>
. You don't actually have a known type because you created anonymous types in your query, so you had to use avar
type variable to hold the results. If you want to be able to create aList<T>
, you need a concrete type:class EmpDetail
{
public int Id { get; set; }
public string EmpName { get; set; }
}// ...
IEnumerable<EmpDetail> empDetails = from emp in mymodel.Employees select new EmpDetail { Id = emp.Id, EmpName = emp.Name };
List<EmpDetail> empDetailsList = empDetails.ToList();This works. :thumbsup: You guys are awesome!!! But I had to create another class for Employee as you mentioned, this is almost replica of mymodel.Employee. There is not much difference between them other than inheriting from INotifyPropertyChanging, INotifyPropertyChanged. Is that the one preventing from converting it to list using ToList()? Any thoughts? Thanks a lot. Muthu.
-
This works. :thumbsup: You guys are awesome!!! But I had to create another class for Employee as you mentioned, this is almost replica of mymodel.Employee. There is not much difference between them other than inheriting from INotifyPropertyChanging, INotifyPropertyChanged. Is that the one preventing from converting it to list using ToList()? Any thoughts? Thanks a lot. Muthu.
If you have an existing Employee entity...just use it. You don't have to create an intermediate class if you don't want one. Type initializers will work on any class that has public get/set properties. Or, if you require an abstracted creation process (such as through a factory, you can call your factory method directly from the LINQ statement):
// Employee entity
class Employee
{
public int ID { get; set; }
public string Name { get; set; }
// ...
}// Employee factory
static class EmployeeFactory
{
static Employee Construct(int id, string first, string last //, ...)
{
Employee emp = new Employee();
emp.ID = id;
emp.Name = last + ", " + first;return emp; }
}
// Query
IEnumerable employees = from e in db.Employee
where e.DateHired > DateTime.Now.AddMonths(-6)
select EmployeeFactory.Construct(e.ID, e.First, e.Last);IList employeeList = employees.ToList();