Hot answers tagged

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 ...
  • 3,026
53 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 ...
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 ...
  • 7,216
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 ...
  • 2,496
34 votes
Accepted

What is the reasoning behind naming of the .NETs Select (Map) and Aggregate (Reduce)?

This mostly comes down to the history of LINQ. LINQ was originally intended to be SQL-like, and used (largely, though not exclusively) to connect to SQL databases. This leads to much of its ...
  • 44.1k
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 ...
  • 108k
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 ...
  • 32.8k
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. ...
  • 5,349
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 ...
  • 108k
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 ...
  • 194k
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 ...
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 ...
  • 32.8k
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#.
  • 35.9k
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 ...
  • 25.7k
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 ...
8 votes
Accepted

Should I write complex Linq queries?

Let's rewrite that without any LINQ or LINQ extensions: var xItems = new List<object>(); foreach(var responseData in filteredItems) { var parentLevelItems = new List<Item>(); ...
  • 6,843
8 votes

What is the reasoning behind naming of the .NETs Select (Map) and Aggregate (Reduce)?

LINQ methods in .Net source.Where(x => condition) .Select(x => projection) were named to be consistent with LINQ query syntax in C# (and VB.NET) from x in source where condition select ...
  • 9,979
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 ...
  • 5,680
6 votes
Accepted

How to write unit tests for a piece of code that has a data context?

In order for unit tests to work you have to be able to mock all the external dependencies to your code (otherwise you're doing an integration test). Your problem is you have direct dependencies to ...
  • 161
6 votes
Accepted

Most efficient LINQ result for Contains() query

You can trivially define an extension method for ToHashSet if that's what you're looking for. public static class Extensions { public static HashSet<T> ToHashSet<T>(this IEnumerable&...
  • 5,763
5 votes
Accepted

How to avoid the LoadBy pattern?

If you're writing a repository, your repository methods should be structured around business processes, not CRUD methods. Writing Linq instead of these methods is not all that difficult, and you can ...
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 ...
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 ...
  • 10.8k
4 votes
Accepted

Efficient way in comparing two lists

Which one is efficient? depends on your definition of efficient and on your data: Which kind of efficient do you mean: "less processing time", "less memory consumption", "less hours needed to ...
  • 7,442
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 ...
  • 898
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 =>...
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 ...
  • 17.5k
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 ...
  • 38.8k
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 ...
  • 32.8k
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 ...
  • 56.1k

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