I Want a "while" Linq Keyword
(Ported from the original post on Mad Props!)
The other day when I was playing with the coin change code kata, I thought I’d try doing it in the keyword-based “sql” style of linq coding. The hurdle I struck (well, not the only hurdle, but a major one) is that not all of the linq extension methods have keyword equivalents. Case in point, the TakeWhile extension method.
What I’d like to see in a future version of C# is for the team to hijack the existing “while” keyword and let us use that. So the code would become:
var solution = from c in coins
orderby c descending
while amount > 0 // <-- THIS
select new
{
Coin = c,
Number = amount / c,
Remainder = (amount %= c)
};
Now, this is missing the final “Where” clause from the previous posts (which filters out any coins that don’t apply), but that’s no big deal.
I’m not sure why the C# team didn’t include the “while” keyword in their longhand linq syntax from the get-go. Perhaps it was difficult to parse, or maybe they were concerned about making a query that “looks” set based use an iterative keyword like that. I think it fits right in! How about you?
Trackbacks
- rimonabantexcellence site title | http://www.rimonabantexcellence.com/t.php?ahr0cdovl21hdhroyw1pbhrvbi5vcmcvd2hpbgutbglucs1rzxl3b3jkp3jldmlzaw9upte=