PC Review


Reply
Thread Tools Rate Thread

Cleanup code for .net 3.5

 
 
Frank Rizzo
Guest
Posts: n/a
 
      18th Jun 2008
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.
 
Reply With Quote
 
 
 
 
Frans Bouma [C# MVP]
Guest
Posts: n/a
 
      19th Jun 2008
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#)
------------------------------------------------------------------------
 
Reply With Quote
 
Frank Rizzo
Guest
Posts: n/a
 
      20th Jun 2008
Frans Bouma [C# MVP] wrote:
> 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.


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.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Jun 2008
On Jun 18, 6:34 pm, Frank Rizzo <n...@none.net> wrote:

<snip>

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Code Cleanup Bernie Deitrick Microsoft Excel Programming 0 3rd Feb 2006 03:27 PM
Re: Code Cleanup Tom Ogilvy Microsoft Excel Programming 0 3rd Feb 2006 03:11 PM
Re: Code Cleanup Don Guillett Microsoft Excel Programming 0 3rd Feb 2006 03:04 PM
Code cleanup help peter.thompson Microsoft Excel Programming 4 22nd Jan 2006 04:49 AM
Code cleanup peter.thompson Microsoft Excel Programming 2 18th Jan 2006 06:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:25 PM.