3

Which way do you prefer?

var apples = fruits.Where(x => x is Apple).Select(x => x as Apple);

---or---

var apples = from x in fruits where x is Apple select x as Apple;

Comments
  • 6
    Definitely the first one, I'm getting tired of language designers trying to make programming look like English. Its harder to read and more annoying to write
  • 6
    Fruit.where(type: :apple)

    Or better yet with Rails scoping:
    Fruit.apples
  • 3
    The first one. To me, the syntax makes things more clear. However, it's a personal preference and I would never change working code that's in the second format or suggest that it should be changed.
  • 0
    @monkeyboy how come is it that most mdsn articles related to linq use the second form? When I learned to use linq I didn't know this was a thing. It just seems unnatural to type sql like syntax straight into code.
  • 2
    Oops. I missed the tags.
  • 0
    Is this a real query language? Like ever heard of DRY, do we still do that?
    X as Y select X as Y where X is Y wtf dude
  • 0
    It looks weird but it's valid syntax
  • 1
    @BlazinDevilify I think they stressed the "sql like" syntax because it was a good way to use people's existing knowledge of SQL to introduce all the new ways of manipulating collections that LINQ provided. Think of it as a marketing approach. A selling line like "All the power of SQL in your code!" makes you think of all the ways in which you use SQL. A line like "A new class called IEnumerable<> that has a bunch of methods" doesn't really drive the point home.
  • 2
    fruits.OfType<Apple>()
  • 1
    @darthkebab hmm nice to know
  • 1
Your Job Suck?
Get a Better Job
Add Comment