September 2007 - Posts
We will soon publish excerpts of the LINQ in Action book.
One of them will probably show you how to query non-generic
collections. Before it becomes available, let me show you an overview
of what it presents.
When you read and hear about LINQ, you almost always see queries
that work on the generic collections provided by the .NET Framework. In
fact, you can use LINQ to Objects with any type that implements IEnumerable<T>. This means that LINQ to Objects will work with your own generic collection types or generic collections from other frameworks.
A problem you may encounter is that not all .NET collections actually implement IEnumerable<T>.
In fact, only strongly-typed collections implement this interface.
Arrays, generic lists and dictionaries are strongly-typed: you can work
with an array of integers, a list of strings or a dictionary of Book objects. The non-generic collections do not implement IEnumerable<T>, but implement IEnumerable. Does this mean that you won’t be able to use LINQ with ArrayList or DataSet objects, for example?
Fortunately, solutions exist. In the following code sample, we use Cast<Book> to convert a non-generic ArrayList collection into a typed-collection of Book objects. This allows us to use the Where or Select operators for example:
ArrayList arrayList = new ArrayList();
arrayList.Add(new Book { Title="LINQ in Action" });
arrayList.Add(new Book { Title="LINQ for Fun" });
arrayList.Add(new Book { Title="Extreme LINQ" });
var titles =
from book in arrayList.Cast<Book>()
where book.Title.Contains("Action")
select book.Title;
The Cast operator casts the elements of a source sequence to a given type.
It’s
interesting to note that you can also use a feature of C# query
expressions. In a C# query expression, an explicitly-typed iteration
variable translates to an invocation of Cast. Here how to use Cast transparently by declaring the book iteration variable as a Book:
var titles =
from Book book in arrayList
where book.Title.Contains("Action")
select book.Title;
Stay tuned to learn more in our upcoming excerpts. Of course, you can also get the full book immediately!
Cross-posted from http://LinqInAction.net
Nice to see that some old ideas start to come to life! Reflexil is not exactly like my Patcher suggestion, but it has some features of it.
Here is the description taken from the Reflexil site:
Reflexil is an assembly editor and runs as a plug-in for Lutz Roeder's Reflector, a great tool for .NET developers. Reflexil is using Mono.Cecil, written by Jb Evain, which is a strategic library for the Mono project.
Reflexil is able to manipulate IL code and save the modified assemblies to disk. Reflexil also supports 'on the fly' c# code injection.
The sketch I had created for Patcher:

We have a forum for LINQ in Action, where current and future readers can post questions related to the book or to LINQ in general. Here is one question we received recently:
The first chapter states "This means that the applications you’ll build using LINQ can run in a “bare” .NET 2.0 runtime!" ....
Is this true? Or will it require our runtimes to install .NET 3.0 ... or .NET 3.5?
That's right, that's what I wrote in the book. But I hadn't really tried to see what can be achieved. So, here is a small test I did with Visual Studio 2008 Beta 2 and that you can reproduce:
- Create a new console application
- Keep only System and System.Core as referenced assemblies
- Set Copy Local to true for System.Core, because it does not exist in .NET 2.0
- Use a LINQ query in the Main method. For example the one below.
- Build
- Copy all the bin output to a machine where only .NET 2.0 is installed
- Run
It should run without any problem. At least it did for me. The sample .NET 3.5 LINQ application I created did run on a machine where neither .NET 3.0 nor .NET 3.5 have been installed ever.
Warning: This may not work in all cases. I know that .NET 2.0 SP1 is installed with .NET 3.5. It may be required under certain circumstances, but I don't know which. In my case, I didn't use .NET 2.0 SP1, and to the best of my knowledge there is not separate installation for it yet anyway.
Update: As Bobby Diaz points out in a comment. .NET 2.0 SP1 is
actually required if you want to use LINQ to SQL on .NET 2.0 because it
contains an updated version of System.dll! See this forum post for more information.
The sample code I used:
class Program
{
static void Main(string[] args)
{
var processes =
from process in System.Diagnostics.Process.GetProcesses()
where process.ProcessName.StartsWith("s")
select new {process.Id, Name = process.ProcessName};
foreach (var process in processes)
Console.WriteLine(process);
}
}
Cross-posted from http://linqinaction.net
Here is a query I received from a user of SharpToolbox.com:
Hi, not sure if you have seen anything like this, but I need something
that can parse my VS.NET C# project and give me a flow chart of all of
the code it encounters. I have inherited a large complex project that
I need to make sense of and a tool like this would be huge. I
currently have a tool called Understand for C++ (Scientific Toolworks) that does the same thing but for some reason they have no
intention of producing a C# version.
Any help you could provide would be greatly appreciated.
Here is the reply I made:
I don't think there is a single tool that provides what you're looking for for C#. But a combination of tools from http://sharptoolbox.com/categories/code-analysers-standards-verifiers may help you.
Here are some tools you can try:
Even if these tools don't produce exactly the kind of information you need, they are helpful when inheriting code.
Additionally, some tools from the following category provide reverse engineering features you may find interesting as well: http://sharptoolbox.com/categories/modeling-architecture-mda-uml
Maybe you know other tools for .NET that can be useful in this kind of situation?
Update: as some readers commented, .NET Reflector is a free tool that you should absolutely check out. It will help you to analyze code. Its plug-ins provide even more features.
More Posts