background preloader

LINQ

Facebook Twitter

Update Operator. Extended LINQ: additional operators for LINQ to objects. In responses to my last week’s post, several readers mentioned LINQ-like operators they implemented themselves.

Extended LINQ: additional operators for LINQ to objects

I also had ideas for operators that would lead to neat solutions for some problems, so I decided to give it some thought and collect up the most useful operators into a reusable library. My goal was to include operators that are simple to use, but applicable to a broad range of problems. I left out operators that I thought were either too complicated to use, or too specific to a particular problem domain. You can download the full source code of the library here (rename the file to ExtendedEnumerable.cs). Read on to find out what it contains. ReadLinesFrom, WriteLinesTo – I/O in LINQ queries LINQ is a great programming model for simple file-processing tasks. Public static IEnumerable<string> ReadLinesFrom(TextReader reader) public static void WriteLinesTo( this IEnumerable<string> lines, TextWriter writer) Isn’t that neat?

A For ... Each Extension for LINQ. A For ...

A For ... Each Extension for LINQ

Each Extension for LINQ A lot of the time, when I write a LINQ statement, I follow it with a For ... Each loop that processes each item in the query: LINQ to CSV library. Contents Introduction This library makes it easy to use CSV files with LINQ queries.

LINQ to CSV library

Its features include: Follows the most common rules for CSV files. Correctly handles data fields that contain commas and line breaks. Requirements To compile the library, you need a C# 2010 compiler or better, such as Visual Studio 2010 or Visual C# 2010 Express Edition. Installation Simply install the NuGet package. Quick Start Reading from a file To make it easier to get an overview, here is the code again that reads from a file, but now in one go: CsvFileDescription inputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true }; CsvContext cc = new CsvContext(); IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription); var productsByName = from p in products orderby p.Name select new { p.Name, p.LaunchDate, p.Price, p.Description }; foreach (Product item in products) { .... }

Bart De Smet: MinLINQ - The Essence of LINQ. As you must know by now, Erk Meijer and team spend time thinking about and discovering the Essence in things. One year ago today, Bart De Smet blogged about the notion of a core set of LINQ operators, MinLINQ, the essence of LINQ. "Hey Bart, what is MinLINQ, exactly? " "MinLINQ is an implementation of the LINQ to Objects Standard Query Operators using a function-oriented layered approach. Based on three essential operators called Ana, Bind and Cata, others are implemented. While the current implementation focuses on IEnumerable<T> exclusively, the same layering can be used to a dual IObservable<T> implementation. " Building a LINQ Provider. By Pedram Rezaei, Microsoft Corporation You can download the code for this article from here.

Building a LINQ Provider

Throughout this article, we will build a simple IQueryable provider similar to the LINQ to SQL provider demonstrating what is needed to build a custom provider that is capable of executing LINQ queries and performing create, update and delete operations. Language-Integrated Query (LINQ) provides a unified querying model for accessing data regardless of where that data is stored. This groundbreaking innovation bridges the gap between the world of objects and the world of data.

If you are new to LINQ, Don Box and Anders Hejlsberg have a great overview here. There are a few articles either on MSDN or on other websites describing the steps required to build a custom LINQ provider – most notably this one. Digging deeper into PLINQ’s internal implementation - Pedram Rezaei. PLINQ is built on top of the Task Parallel Library (TPL) and promises to revolutionise the way we write programs that can benefit from the multi-core processor era.

Digging deeper into PLINQ’s internal implementation - Pedram Rezaei

But how does it work internally? This article assumes that you are familiar with the basics of LINQ and have an understanding PLINQ and TPL. In this short article, I will concentrate on the techniques used by the first CTP of PLINQ to partition work streams and associate different partitions to different threads. It is worth mentioning that by default, TPL provides one thread per processor core. The deferred execution and lazy evaluation characteristics of LINQ allow for creation of infinite size lists as the source for a LINQ query.

Class NaturalNumbersEnumerable : IEnumerable<uint> public IEnumerator<uint> GetEnumerator() uint i = 1; while (true) yield return i++; IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); var linq = new NaturalNumbersEnumerable() .Take(100); LINQ 101 Samples - Lambda Style. LINQ Extensions Library - Home.