Alex Rivera | Logout

Addicted to LINQ

Asked 2008-11-04T15:15:27.020
11

Ok, the more I use LINQ, the more I like it! I recently found myself working in some legacy code at work. It is your classic DataSet and DataTable rich application. Well, when adding a bit of functionality I found myself really wanting to just query the rows of a DataTable for the results I was looking for.

Let me repeat that... instead of looping and adding to a temp collection, I just wanted to ask the Rows collection for what I needed. No looping, no temp variables, just give me what I want.

var customerOrderIds = table.Rows.Cast<DataRow>()
   .Where(x => (string)x["CUSTOMER_ID"] == customerId)
   .Select(x => (string)x["CUSTOMER_ORDER_ID"])
   .Distinct();

My question is whether or not this is a good thing, or am getting carried away with LINQ? It does seem to me that this declarative style of pulling a subset of data out of a collection is a good thing and more readable in the end. But then again, maybe I'm just smitten :)

Edit
Report

2 Answers

0

LINQ is simply writing the "looping/temp variable" code for you. LINQ helps you to write code faster (and more readable).

You're code is good.

answered 2008-11-04T15:30:55.990
0

A join (using the join keyword, but not the from keyword) uses a Dictionary for the matches and is thus O(M+N).

So is a group by, but not the following:

from x in Xs
from y in Ys
    .Where(o => o == x)
select new
{
    x,
    y
}

which is O(M*N).

answered 2009-08-07T15:37:14.967

Your Answer