Live data from Hacker News

Best Practices for Linq Enumerables and Queryables

code.jonwagner.com

21–30 of 30 posts

Re: Best Practices for Linq Enumerables and Queryables

#21
post #9

Earlier quoted context omitted.

Regarding the let keyword; the following code: var x = from post in posts let keywords = post.split(' ') ... Is compiled* into: var x = posts .Select(post => new { keywords = post.split(' '), post }) ... * If you didn't know already, the compiler transforms query syntax into extension method syntax.

Oh yes, I did actually realise that. Thank you though. I worded it badly. I should have been clearer in that I was following on from solutionyogi's argument about readability. The compiler example is a bit on the ugly side, wouldn't you say? To then access 'keywords', it becomes ... .Where(anon => anon.keywords[0] == "verybadexample") .Select(anon => anon.post); What I should have said was that I'm not sure how you w…

Like this:

  var thing = 
    from x in stuff
    let derp = x.herp
    select { x.name }
Equals this:

  var thing = stuff.Select( x => {
    var derp = x.herp;

    return new { x.name };
  } );
edit: formatting.

I think each have their place, but this absolutely enrages me:

  var things = ( from x in thingList select x ).ToList()

Re: Best Practices for Linq Enumerables and Queryables

#22
post #5

Earlier quoted context omitted.

About your first point, sometimes using method syntax is more readable than query. When you are not reliying heavily on LINQ and just use some commands, is easier to write var males = customers.Where(c => c.Gender == "male"); than var males = from c in customers where c == "male" select c; Not only because it's longer, but also because it can feel strange if you're not using it continously.

Readability is definitely subjective. For simple scenarios, I do prefer the extension method. E.g. in my code, it would actually be like var males = customers.Where(c => c.IsMale); vs var males = from c in customers where c.IsMale select c; But often, queries are not that simple and in such scenario query syntax offers far more readability: e.g. var filteredCustomers = from c in customers join o in orders on o.custom…

The corresponding extension method syntax is:

  var filteredCustomers = customers
      .Join(orders,
          c => c.customerid,
          o => o.customerid,
          (c, o) => new { Customer = c, Order = o }
      )
      .Where(x => x.Customer.IsMale && x.Customer.Age > 30)
      .Where(x => x.Order.IsPending);

Re: Best Practices for Linq Enumerables and Queryables

#23

Earlier quoted context omitted.

I think perhaps he is confusing best practices for public interfaces with general-purpose best practices. It is a good idea to prefer ToList()ing any data you're passing out of a library. An 'open' LINQ query might represent a whole lot of work, and that work will get repeated every time someone re-enumerates the query. And the query might be holding on to any number of resources that the end-user can't know about. R…

But if you're ToListing it, your return type might as well just be List, not IEnumerable (or IQueryable). I feel that by declaring your return type as IEnumerable, you're implicitly saying to any caller that the return object is something that can iterate (and potentially generate) through results when requested, and so care should be taken with its use (to avoid getting multiple IEnumerator objects, and iterating un…

But if you're ToListing it, your return type might as well just be List, not IEnumerable

Perhaps, it really depends. One nice advantage that returning IEnumerable has over returning List is that it gives better flexibility and maintainability.

If you return List, you're tying yourself to that specific class now and forever. Any change will be a breaking change.

If you return IEnumerable, all you're guaranteeing is that you'll return something that the caller can enumerate over to get their data. Meaning if you later discover that you have some compelling reason to switch to using a HashSet internally, and that it would also be most convenient if you could just pass back that HashSet, well, there's nothing to stop you.

You don't get that flexibility by typing your return value as List because you've tied yourself to that specific class. You also don't get that flexibility by passing back IList. IList defines an ordered, positionally-indexed collection, and hashes are not that. ICollection might work, but it defines an interface for a mutable collection, which might also be a restriction you don't want to commit to now and forever.

So in general it's best to pass back the most flexible type you can. Partially because YAGNI, but mostly because trying to create a pit of success for your users doesn't mean you can't also try to create a pit of success for yourself as well.

(Forgot to mention - the semantics that you're claiming for IEnumerable doesn't really line up with how it's actually used. IEnumerable has been around since .NET 1.1, and IEnumerable has been around since .NET 2.0. There were years and years where IEnumerable simply defined an object that could be enumerated before LINQ came on the scene and introduced us to ubiquitous examples of lazily-generated IEnumerables, or introduced all these useful extension methods that take IEnumerable and return a lazily-generated IEnumerable.)

Re: Best Practices for Linq Enumerables and Queryables

#24

Earlier quoted context omitted.

But if you're ToListing it, your return type might as well just be List, not IEnumerable (or IQueryable). I feel that by declaring your return type as IEnumerable, you're implicitly saying to any caller that the return object is something that can iterate (and potentially generate) through results when requested, and so care should be taken with its use (to avoid getting multiple IEnumerator objects, and iterating un…

But if you're ToListing it, your return type might as well just be List, not IEnumerable Perhaps, it really depends. One nice advantage that returning IEnumerable has over returning List is that it gives better flexibility and maintainability. If you return List , you're tying yourself to that specific class now and forever. Any change will be a breaking change. If you return IEnumerable , all you're guaranteeing is…

Definitely. Should have left that first line out, as it wasn't what I was trying to argue. The rest of my point still stands.

Edit: I see you've appended to your comment. The problem is you could always have lazily-evaluated IEnumerables by implementing an IEnumerator. It was just a pain in the arse until C#2 brought us generator support through the yield keyword. This was long before Linq came along.

Edit2:

  > There were years and years where IEnumerable simply defined an object that could be enumerated...
Which is my point exactly. And nothing has changed with IEnumerable (generics excluded). It certainly doesn't say that all the objects are already held in-memory (as enforcing ToList would do). By returning an IEnumerable, you're just saying here's an object that can produce you a sequence of results. In a public API, it should be documented (at least some vague allusion to) whether this will be produced by trivially pulling them out of an in-memory list, or whether something a bit more clever is going on, as there'll certainly be occasions where streaming the results through a generator is more desirable than holding them all in memory.

Re: Best Practices for Linq Enumerables and Queryables

#25
Uhg. Always defer materializing the query until you need it. If you are using LINQ against something like EF it changes how the SQL is generated.

Consider var query = (from customer in context.Customers select customer).ToList().Where( c => c.Id == 3);

vs var query = (from customer in context.Customers where customer.Id == 3 select customer);

The first LINQ query results in "SELECT * FROM customer". The WHERE clause is applied after the result set is returned. The second generates a real WHERE clause. The result set is filter on the server before being sent to the client.

Re: Best Practices for Linq Enumerables and Queryables

#26
Thanks for the feedback. I've made some updates to the article to hopefully improve where my meaning was not as clear as I intended, and where I should have more strongly written "it depends on your situation".

My next controversial post will be titled "Proper Spacing Around Parentheses in C#" ;)

Re: Best Practices for Linq Enumerables and Queryables

#27
post #4

Earlier quoted context omitted.

I agree--I'm not sure how he was lead to believe this is best practice. If you mean to process the results of the query with a loop, its silly to enumerate over the query to create a List then enumerate over it again to modify those objects, which sounds like what he's suggesting is best practice.

I think perhaps he is confusing best practices for public interfaces with general-purpose best practices. It is a good idea to prefer ToList()ing any data you're passing out of a library. An 'open' LINQ query might represent a whole lot of work, and that work will get repeated every time someone re-enumerates the query. And the query might be holding on to any number of resources that the end-user can't know about. R…

> I think perhaps he is confusing best practices for public interfaces with general-purpose best practices.

Exactly. Most of the advice in the article is sensible when viewed from that perspective.

I use LINQ to Entity in my company's service API. If you fail to "seal" the query (as he calls it) with ToList() before you leave the 'using' block for your DB context, your callers get a runtime error later since the IQueryable isn't run until the caller enumerates it (i.e. after the 'using' block has disposed the DB context). (On the other hand, if you call ToList() too early in your method chain, you're preventing L2E from composing your expression tree into optimal SQL, since everything after ToList() is run client-side.)

As an API provider you have to treat the behavior of your return values as part of the contract, and if your caller is expecting a plain IEnumerable (like you claim to return in your method signature), you'd best make sure it isn't really an IEnumerableThatDependsOnNondeterministicContext or an IEnumerableWithUnpredictableSideEffects.

His point is spot on about returning IQueryable only when your intention is for the caller to compose your result with other queries. If you're just returning an IQueryable because you think it's cool to defer execution, you're probably missing the point. Deferred execution isn't 100% win; you can just as easily defer yourself into a timeslot when there's more contention for a resource as into one where there's less contention. In many cases it's just as well to declare your need for the data (e.g. by calling ToList() as early as possible and let the system manage the execution.

Re: Best Practices for Linq Enumerables and Queryables

#28
post #5

Earlier quoted context omitted.

About your first point, sometimes using method syntax is more readable than query. When you are not reliying heavily on LINQ and just use some commands, is easier to write var males = customers.Where(c => c.Gender == "male"); than var males = from c in customers where c == "male" select c; Not only because it's longer, but also because it can feel strange if you're not using it continously.

Readability is definitely subjective. For simple scenarios, I do prefer the extension method. E.g. in my code, it would actually be like var males = customers.Where(c => c.IsMale); vs var males = from c in customers where c.IsMale select c; But often, queries are not that simple and in such scenario query syntax offers far more readability: e.g. var filteredCustomers = from c in customers join o in orders on o.custom…

I've used two from's for inner joins:

    var filteredCustomers =
       from c in customers
       from o in orders
       where o.customerid = c.customerid &&
             c.IsMale && c.Age > 30 &&
             o.IsPending
       select new {Customer = c, Order = o};
This is one of those cases where I'll favor query syntax over extensions. OTOH, query syntax requires an explicit `select` where it's often optional in extensions. Also, `.ToList()` doesn't have a query syntax counterpart so you'll often mix both if you tend to favor query.

Re: Best Practices for Linq Enumerables and Queryables

#30
post #27

Earlier quoted context omitted.

I think perhaps he is confusing best practices for public interfaces with general-purpose best practices. It is a good idea to prefer ToList()ing any data you're passing out of a library. An 'open' LINQ query might represent a whole lot of work, and that work will get repeated every time someone re-enumerates the query. And the query might be holding on to any number of resources that the end-user can't know about. R…

> I think perhaps he is confusing best practices for public interfaces with general-purpose best practices. Exactly. Most of the advice in the article is sensible when viewed from that perspective. I use LINQ to Entity in my company's service API. If you fail to "seal" the query (as he calls it) with ToList() before you leave the 'using' block for your DB context, your callers get a runtime error later since the IQue…

That reminds me of another one I ran into. It's not specifically LINQ related, but does play into the problem with returning generators. A homegrown data access layer that would grab a SqlDataReader and then yield each item.

That ended up creating all sorts of problems, up to and including a serious deadlocking issue at the database end of things.

Post reply on HN