471,349 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Cleanup code for .net 3.5

Ok, I have a simple loop that creates a List<Employeefrom 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.
Jun 27 '08 #1
3 1132
Frank Rizzo wrote:
Ok, I have a simple loop that creates a List<Employeefrom 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#)
------------------------------------------------------------------------
Jun 27 '08 #2
Frans Bouma [C# MVP] wrote:
Frank Rizzo wrote:
>Ok, I have a simple loop that creates a List<Employeefrom 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.
Jun 27 '08 #3
On Jun 18, 6:34 pm, Frank Rizzo <n...@none.netwrote:

<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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by use dmgass at hotmail dot com | last post: by
1 post views Thread by Carl J. Van Arsdall | last post: by
6 posts views Thread by Rémi | last post: by
1 post views Thread by Jason S | last post: by
69 posts views Thread by MQ | last post: by
3 posts views Thread by Petr Pavlu | last post: by
5 posts views Thread by knyghtfyre | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.