[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Linq Var to List<>

Posted on 2009-02-12
5
Medium Priority
?
7,869 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

0
Comment
Question by:u2envy1
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23620435
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
 
LVL 53

Expert Comment

by:Dhaest
ID: 23620443
Otherewise:
List<string> results = new List<string>();
foreach (var item in query)
    results.Add(item.Title);
0
 

Author Comment

by:u2envy1
ID: 23620507
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
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23620525
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
 

Author Closing Comment

by:u2envy1
ID: 31545972
thx, that solved my problem.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question