Skip to main content
Share Your Experience: Take the 2024 Developer Survey
161 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

I have a bag with five potatoes in it. Are there .Any() potatoes in the bag? "Yes," you say. <= true I take all of the potatoes out and eat them. Are there .Any() potatoes in the bag? "No," you ...
Dan Wilson's user avatar
  • 3,103
55 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

First off, it appears that that source code will throw ArgumentNullException, not NullReferenceException. Having said that, in many cases you already know that your collection is not null, because ...
Mason Wheeler's user avatar
51 votes
Accepted

What advantage was gained by implementing LINQ in a way that does not cache the results?

What advantage was gained by implementing LINQ in a way that does not cache the results? Caching the results would simply not work for everybody. As long as you have tiny amounts of data, great. Good ...
nvoigt's user avatar
  • 8,034
37 votes
Accepted

Unit tests: deferred assertions with Linq

Is it ok to add deferred assertions like this [..] No, it isn't. Why? Because if you for any reason remove the second assert the test would still turn green and you would think it still works but it ...
t3chb0t's user avatar
  • 2,554
25 votes

What advantage was gained by implementing LINQ in a way that does not cache the results?

What advantage did Microsoft hope to gain by implementing it this way? Correctness? I mean, the core enumerable can change in between calls. Caching it would produce incorrect results and open the ...
Telastyn's user avatar
  • 109k
22 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

Null means missing information, not no elements. You might consider more broadly avoiding null — for example, use one of the built-in empty enumerables to represent a collection with no ...
Erik Eidt's user avatar
  • 34.1k
14 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

Aside from the null-conditional syntax, there is another technique to alleviate this problem: don't let your variable ever remain null. Consider a function that accepts a collection as a parameter. ...
jpmc26's user avatar
  • 5,409
13 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

Am I wrong, as a client of this code, to expect that e.g. ((int[])null).Any() should return false? Yes, simply because you're in C# and that behavior is well defined and documented. If you were ...
Telastyn's user avatar
  • 109k
11 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

If the repeated null-checks annoy you, you could create your own 'IsNullOrEmpty()' extension method, to mirror the String method by that name, and wrap both the null-check and the .Any() call into a ...
Theo Brinkman's user avatar
11 votes

Name for a Chainable ForEach

Linq functions with modifying effects will at least violate the principle of least astonishment, so what you have in mind is a code smell, to my understanding. For your use case, I think it is much ...
Doc Brown's user avatar
  • 208k
10 votes
Accepted

Reactive Programming in C# - how to roll my own?

That is not reactive programming. Reactive programming is something entirely different. What you have is called fluent interface. And it is quite trivial to implement in C#.
Euphoric's user avatar
  • 37.4k
10 votes

Null or empty object when LINQ to Entities query returns nothing

Database queries return result sets. An empty set is a reasonable answer; it means you don't have any of the things searched for. I would argue that your code is going to far in canonicalization ...
Erik Eidt's user avatar
  • 34.1k
10 votes
Accepted

Replacing Linq Methods with Extension Methods

Extension methods offer you an opportunity to reason about problems a totally different way-- functionally. Functional programming is a totally different paradigm from object-oriented and has certain ...
John Wu's user avatar
  • 26.5k
9 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

Am I wrong, as a client of this code, to expect that e.g. ((int[])null).Any() should return false? If you wonder about expectations you have to think about intentions. null means something very ...
Søren D. Ptæus's user avatar
7 votes

Import large csv files

A Stream does not need to read the entire file into memory, so... Don't bother with Excel at all if you can accumulate needed information while reading records: Sum as records are read Build a ...
radarbob's user avatar
  • 5,823
7 votes

Sorting a sequence according to a hierarchical set of rules

How to tackle your specific example In cases where you have individual rules for each category, those categories must be known at programming time. Things might become more complicated with dynamic ...
Doc Brown's user avatar
  • 208k
5 votes
Accepted

What makes LINQ (C#) unique compared to another DSL such as Django query syntax?

What makes LINQ (C#) unique compared to another DSL such as Django query syntax? Simply put: what makes C# Language INtegrated Queries unique is that it is a Query Language that is Integrated into ...
Jörg W Mittag's user avatar
4 votes

Import large csv files

Ummm...Use Excel. Excel can import CSV files. All the functionality you describe here can be done in Excel. If these are recurring tasks, you could write some macros to automate the loading of the ...
Jon Raynor's user avatar
  • 11.5k
4 votes

What advantage was gained by implementing LINQ in a way that does not cache the results?

Another reason that hasn't been mentioned is, the possibility of concatenating different filters and transformations without creating garbage middle results. Take this for example: cars.Where(c =>...
Arturo Torres Sánchez's user avatar
4 votes

What advantage was gained by implementing LINQ in a way that does not cache the results?

Because LINQ is, and was intended from the beginning to be, a generic implementation of the Monad pattern popular in functional programming languages, and a Monad is not constrained to always yield ...
Jules's user avatar
  • 17.8k
4 votes
Accepted

Import large csv files

I do not know which, from the other answers, will work best for you. AFAICT, I'd remark, mostly from an implementation standpoint, that they all make sense, but from a design perspective, which one ...
YSharp's user avatar
  • 888
4 votes

Replacing Linq Methods with Extension Methods

Extension methods are static with all the cons implied, Because an extension method is just a static method that takes this as its first parameter and doesn't expose the inner workings of this, it ...
David Arno's user avatar
  • 39.3k
3 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

There are many answers explaining why null and empty are different and enough opinions trying to explain both why they should be treated differently or not. However you're asking: Am I wrong, as a ...
Adriano Repetti's user avatar
3 votes

Null or empty object when LINQ to Entities query returns nothing

It depends! But first a clarification: .DefaultIfEmpty().First() can be written simpler as .FirstOrDefault() which does the same - returns the first item, or null if the result is empty. But you ...
JacquesB's user avatar
  • 59.7k
3 votes

What advantage was gained by implementing LINQ in a way that does not cache the results?

Fundamentally, this code — putting a Guid.NewGuid () inside a Select statement — is highly suspicious. This is surely a code smell of some kind! In theory, we would not necessarily ...
Erik Eidt's user avatar
  • 34.1k
3 votes

Import large csv files

Of course you can use bulk import. If you can't access the corporate database for some reasons, you install one locally and use it. Since you seem to be working with Microsoft's stack, SQL Server ...
Arseni Mourzenko's user avatar
3 votes

Name for a Chainable ForEach

The MoreLINQ library has this method and calls it Pipe, which I find very appropriate. Pipe   |   Executes the given action on each element in the source sequence and yields it If you're ...
Gediminas Masaitis's user avatar
3 votes

The best way to convert Dynamic SQL to C#/Linq while joining multiple tables

Your Dynamic SQL has: One optional JOIN clause (When addressID is set) - but is never projected in the SELECT Multiple optional WHERE clauses - all AND appended Therefore, you don't need a query ...
Kind Contributor's user avatar
3 votes

What makes LINQ (C#) unique compared to another DSL such as Django query syntax?

While Linq is certainly an attractive feature to consider on its own merits, the real value of Linq is all of the other powerful language features that were added to the C# language and framework that ...
Robert Harvey's user avatar
2 votes
Accepted

How do you create a Linq IQueryable Where query with string predicate and params object[] arguments

The code to accomplish what you are asking is large, it is easier to use NuGet package NequeoLinq. Sample code, first add the extension using Nequeo.Extension; using Nequeo.Linq.Extension; On any ...
D.Zadravec's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible