Posts Tagged ‘LinQ’

Reactive Extensions (Rx): intro video from NDC 2012 by Paul Betts

November 14, 2012

From Rx web site:

The Reactive Extensions (Rx) is a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.

Paull Betts introduce Rx (with Linq) as a new tool for developers, a library available via NuGet (here) and the power of IObservable<T>+ Linq. 🙂

source: Introduction to Rx
source: Paull Betts

Technorati tags: Rx,Linq,IObservable

LINQ: List of Lists to flattern

May 21, 2012

Lots of code contains list of lists or dictionary of lists data objects.
Quite often we need to extract a flattern list of data.

The simplest linq staments is

var flatterList = from el in externalList
                  from il in el.innerList
                  select el.code, el.description, il.subcode, il.detailA;

For a complete list of samples, read Use LINQ’s SelectMany Method to “Flatten” Collections .

source: Use LINQ’s SelectMany Method to “Flatten” Collections

Technorati tags: LINQ

Exploring LinQ

June 23, 2010

LinQ is very powerfull, really really powerfull.

Referring to my last post (LinQ: Max() vs OrderBy()-FirstOrDefault()) here is a similar example:

var i = (from item in listOfItems
where item.Size = listOfItems.Max(i => i.Size)
select item);

What about performance ?
What about that “listOfItems.Max(i => i.Size)” ?
Will be executed every iteration, or cached before cycle ?
Reflector may help you exploring the code LinQ is compiled in.

Dustin Campbell had same idea, with this post. 😉

source: Using Reflector to Explore LINQ

LinQ: Max() vs OrderBy()-FirstOrDefault()

June 16, 2010

What about this:

var i = (from item in listOfItems
select item).Max();

And what about if listOfItems is empty ?
Max() throws an exceptions, then you change to:

var i = (from item in listOfItems
order by item descending
select item).FirstOrDefault();

but, the last statement has higher cpu usage.

Jon Skeet in A short case study in LINQ efficiency made a performance test very interesting.

Technorati tags: LinQ

Linq2SQL: “NOT IN” SQL statement

March 23, 2010

Just a remind for myself. 😉

“NOT IN” SQL clause is a very common sql statement in most queries.
Is there something similar in Linq?  Of course!
Marco Russo’s blog post explain it very well.

Here as example from Marco’s blog:
SQL query

SELECT * FROM CUSTOMERS
WHERE ID NOT IN
(SELECT CUSTOMERID
FROM ORDERS
)

translated to Linq query:

var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;

source: The NOT IN clause in LINQ to SQL.

Technorati tags: Linq, Linq2SQL, SQL