Linq Var to List<>

How do I copy a Var to a List<int> ?
var rsEmpList = (from t in Empctx.Employees
                                    where item.EmployeeID == employeeID
                                     select new
                                 {
                                     t.DailyHoursID
                                 }).ToList();
 
 List<Int32> empCatList = new List<Int32>();
empCatList.AddRange(rsEmpList);

Open in new window

u2envy1Asked:
Who is Participating?
 
DhaestCommented:
Either choose a list of long
List<Long> empCatList = new List<Long>();
                    foreach (var item in rsEmpList)
                    {
                        empCatList.Add(item.DailyHoursID);
                    }

Or cast your result

List<Int32> empCatList = new List<Int32>();
                    foreach (var item in rsEmpList)
                    {
                        empCatList.Add((int)item.DailyHoursID);
                    }
0
 
DhaestCommented:
Did you try the following
var rsEmpList = (from t in Empctx.Employees
                                    where item.EmployeeID == employeeID
                                     select new
                                 {
                                     t.DailyHoursID
                                 }).ToList();
 
 List<Int32> empCatList = new List<Int32>();
empCatList = rsEmpList.ToList();

Open in new window

0
 
DhaestCommented:
Otherewise:
List<string> results = new List<string>();
foreach (var item in query)
    results.Add(item.Title);
0
 
u2envy1Author Commented:
I get an error when doing this...
Argument '1': cannot convert from 'long' to 'int'
I need the list to contain int values.
 List<Int32> empCatList = new List<Int32>();
                    foreach (var item in rsEmpList)
                    {
                        empCatList.Add(item.DailyHoursID);
                    }

Open in new window

0
 
u2envy1Author Commented:
thx, that solved my problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.