[.net] What's a good beginner's resource to learn Linq?

Started by
26 comments, last by Narf the Mouse 14 years ago
It's also gone way off-topic, about ~14 posts that don't have another to do with beginner's resources for Linq.

So; does anyone have more advice in that area?
Advertisement
That's largely because you haven't really mentioned what about the previously-linked resources or the obvious search results are insufficient for your needs.
Ah, sorry. I have been looking on MSDN; I've learned some stuff there. And the linked "101 Samples" were excellent; I'm keeping that for future reference. But I'm still not sure what all I can do with Expression Trees.

As for Google, it's inferior to input from individual humans, I've found. Still, I do use it.
Expression trees are a fairly advanced topic, and won't see too much use by most people. They're also part of linq, but often considered a separate topic.

Essentially, they're a data representation for functions. You can build and modify the expression tree in memory, and then 'compile' them into a proper delegate that you can then invoke. Lambdas are converted into them (and you can iirc assign a lambda expression to an Expression variable).

They end up being used for runtime code generation, and are much more performant than reflection since (once compiled) expression trees cost the same as any other delegate invocation.

I've used them for dynamically wiring up grids based on reflection info, doing a poor-man's rules engine (storing snippets of a simple domain language in database, then compiling them on the fly), and a few other little reflection bindings. They're way better than the alternatives and pretty easy to use.
How would you assign a lambda expression to an expression variable? My attempts at the obvious so far have failed. (Assuming the standard format of "Value => { code; }")

Thanks.
Quote:Original post by Narf the Mouse
How would you assign a lambda expression to an expression variable? My attempts at the obvious so far have failed. (Assuming the standard format of "Value => { code; }")

Thanks.


Expression<Func<bool>> expr = () => false;


This is of course the trivial example. Different function signatures will take different parameters to expression, and be more complex lambdas.

Quote:Original post by Mike.Popoloski
Without LINQ I couldn't do stuff like this:

public Board(string input) : this(input.ToCharArray().Select(c => "Xx. -".Contains(c) ? '0' : c).Where(c => char.IsNumber(c)).Select(c => new Cell() { Value = c - '0' }))

or

foreach(var range in ranges) results.Add(ranges.Aggregate(range, (current, item) => (current.Contains(item.Lower) ^ current.Contains(item.Upper)) ? new Range(Math.Min(current.Lower, item.Lower), Math.Max(current.Upper, item.Upper)) : item.Lower < current.Lower && item.Upper > current.Upper ? item : current));

Where would I be without LINQ? [grin]


Probably in the "reasonable code" realm, and not the "Mike-Popoloski-dig-into-every-nook-of-C#'s-features-then-delete-the-line-breaks realm." :D
Quote:Original post by Telastyn
Quote:Original post by Narf the Mouse
How would you assign a lambda expression to an expression variable? My attempts at the obvious so far have failed. (Assuming the standard format of "Value => { code; }")

Thanks.


Expression<Func<bool>> expr = () => false;


This is of course the trivial example. Different function signatures will take different parameters to expression, and be more complex lambdas.

Thanks. Would have said that sooner, but life is not being easy right now.

This topic is closed to new replies.

Advertisement