We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Linq Var to List<>

u2envy1
u2envy1 asked
on
Medium Priority
10,578 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
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

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
Otherewise:
List<string> results = new List<string>();
foreach (var item in query)
    results.Add(item.Title);

Author

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

Project manager
CERTIFIED EXPERT
Commented:
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);
                    }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thx, that solved my problem.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.