472,980 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,980 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 1171
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: use dmgass at hotmail dot com | last post by:
I'm writing a module and when it is imported by a script I want some code automatically executed when the importing script is finished executing. I'd like it to execute before interactive mode is...
1
by: Carl J. Van Arsdall | last post by:
I'm extending Python with C, it seems as though initialization is easy enough with an init<moduleName>() function that all C extended python modules must provide that can serve as a sort of "module...
6
by: g35rider | last post by:
Hi, sometimes due to unchecked code I get segmentation faults once in a while and would like to be able to catch them and do some cleanup on things. This segmentation fault could be anywhere in the...
6
by: Rémi | last post by:
Can someone tell me whether or not my assumptions are good? Just want to know if I'm going to run into trouble in how I'm deleting map entries. Although I like to think I know my way around C++,...
1
by: Jason S | last post by:
I haven't used try/catch/finally very much in Javascript. My function (let's call it try_it()) needs to call a function that could throw an exception (let's call it dangerous()) with some setup()...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
3
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1,...
5
by: knyghtfyre | last post by:
Hello, My company is developing a rather large application with .NET 2.0. We are expanding to a server farm and are in the process of converting our application to use an out-of-process session...
6
by: Peter Michaux | last post by:
I just ran some circular memory leak tests in IE6, O9, S3, FF2 and it seems to me they all benefit from doing the same kind of circular memory leak cleanup that IE requires. My tests were very...
4
by: IanWright | last post by:
I've got a section of a program that I can't quite get to work. I'm fairly sure its something very simple/trivial but it looks correct to me, so if someone could help me fix the problem, and explain...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.