Cleanup code for .net 3.5

F

Frank Rizzo

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?

Thanks.
 
F

Frans Bouma [C# MVP]

Frank said:
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#)
------------------------------------------------------------------------
 
F

Frank Rizzo

Frans said:
Frank said:
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.

Why? Because this is a minor project where performance is not
important. And because I am using this project to learn all the 3.5
goodies.
 
J

Jon Skeet [C# MVP]

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?

Off the cuff, that looks okay. I probably wouldn't use a full query
expression, just a call to Select, but that's a matter of aesthetics.

When you say it doesn't work, what exactly do you mean? What's
happening when you run the above code? If it's complaining that dr is
of type object (and therefore doesn't have an indexer) try changing it
to "from DataRow dr". If that doesn't work, please post a short but
complete program which demonstrates the problem.

There are some extensions to ADO.NET to make LINQ work more smoothly,
but I can't remember whether any of them are for untyped data sets off
hand.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top