Frank Rizzo wrote:
> Ok, I have a simple loop that creates a List<Employee> from some fields
> in the DataTable:
>
> List<Employee> _TicketList = null;
> DataTable TicketTable = GetTicketList();
>
> foreach (DataRow dr in _TicketTable.Rows)
> {
> Employee empl = new Employee
> {
> Name = dr["AssignedTo"].ToString(),
> Tickets = Convert.ToInt32(dr["InProcess"])
> };
>
> _TicketList.Add(empl);
> }
>
>
> How can I do the same thing using LINQ. I've tried several variations,
> but none of them seem to work:
>
> List<Employee> _TicketList = (from dr in _TicketTable.Rows
> select new Employee { Name = dr["AssignedTo"].ToString(),
> Tickets = Convert.ToInt32(dr["InProcess"])})
> .ToList<Employee>();
>
> What am I missing? Or does DataRowCollection simply not implement LINQ?
Why would you want to rewrite code in Linq if it already works
perfectly? Keep in mind that Linq to Objects queries are often slower
than normal for loops for example, and it's not always more readable.
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------