Please consider the following sample, consisting of a definition of a nested XElement and a pair of Linq expressions. The first expression, which works as expected, iteratively fetches the first and last XElements at the bottom level by selecting over a tmp made by getting the bots (for bottoms), stored in new instances of an anonymous type for reuse of the name "bots." The second expression attempts to do the same thing, just using a "Let", but it does not work at all. First, the compiler complains about type inference not working, and then, when I put in explicit types, it goes off into IObservable and gets even more lost. I expected this to be utterly straightforward and was quite surprised and flummoxed at the failure. I'd be grateful if anyone has a moment to look and advise. You can paste the following into LinqPad, add a reference to System.Interactive, and see the failed compilation.
var root = new XElement("root",
new XElement("sub",
new XElement("bot", new XAttribute("foo", 1)),
new XElement("bot", new XAttribute("foo", 2))),
new XElement("sub",
new XElement("bot", new XAttribute("foo", 3)),
new XElement("bot", new XAttribute("foo", 4))));
root.Dump("root");
root.Descendants("sub")
.Select(sub => new {bots = sub.Descendants("bot")})
.Select(tmp => new{fst = tmp.bots.First(), snd = tmp.bots.Last()})
.Dump("bottoms")
;
root.Descendants("sub")
.Select(sub => sub.Descendants("bot"))
.Let(bots => new{fst = bots.First(), snd = bots.Last()})
.Dump("bottoms2")
;
let
is just a keyword that simplifies transforms liketmp => new{fst = tmp.bots.First(), snd = tmp.bots.Last()}
. Rather than using aLet
extension method, just use theSelect
method: