What is the worst abuse you've seen of LINQ syntax? [closed]

回眸只為那壹抹淺笑 提交于 2020-01-01 02:48:26

问题


On a recent Dot Net Rocks podcast, Jon Skeet mentioned possible abuses of LINQ syntax. What examples have people seen where crazy things are being done with LINQ?


回答1:


It has to be a ray-tracer implemented in a single LINQ expression. Clever, beautiful, and scary all at the same time!




回答2:


Here are my own abuses - purely for the sake of having a laugh at a geek night, and demonstrating what the compiler actually does with query expressions.

Arguably my "LINQ to Mandelbrot" is a bit abusive too :)

I was particularly thinking of abuse of the syntax by the way, but there are always plenty of ways to abuse the very presence of LINQ - doing things "the LINQ way" when there are simpler approaches available without using LINQ. For instance, getting to the nth element of an array:

// Sensible (we know that people implements IList<Person>)
Person x = people[10];
// Insane
Person y = people.Skip(9).First();

I suspect there will be more cases of abuse like this than abusing the power of query expressions, partly because many devs won't realise that abusing query expressions will even work :)




回答3:


Honestly, it's got to be cases where people chose the LINQ syntax where the code to do so:

  • Was the same or longer than a simple loop
  • Offered no performance or correctness advantage (readability/maintainability) over a simple loop



回答4:


One of the "best" ones I've ever seen is from thedailywtf.com

public string LastSixDigits
{
 get
 {
   if (string.IsNullOrWhiteSpace(this.Number) || this.Number.Length < 6)
     return string.Empty;
   return this.Number.Reverse().Take(6).Reverse().Aggregate(string.Empty, (s, c) => s += c);
 }
}


来源:https://stackoverflow.com/questions/246270/what-is-the-worst-abuse-youve-seen-of-linq-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!