Hot answers tagged

162 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,045
54 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
  • 7,226
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,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 ...
Jerry Coffin's user avatar
  • 44.2k
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
  • 33k
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,389
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

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
  • 196k
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
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
  • 36.1k
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
  • 33k
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
  • 25.8k
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
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>(); ...
Ben Aaronson's user avatar
  • 6,853
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 ...
svick's user avatar
  • 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 ...
radarbob's user avatar
  • 5,800
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 ...
Robert Harvey's user avatar
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
  • 10.9k
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 ...
k3b's user avatar
  • 7,468
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
  • 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 =>...
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.6k
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
  • 38.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 ...
JacquesB's user avatar
  • 56.9k
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
  • 33k
3 votes

how to do temporary tables in Linq to Entity Framework

Honestly, why force a square peg into a round hole? Entity Framework supports calling stored procedures for a reason; because Entity Framework cannot address every use case that the database itself ...
Craig Tullis's user avatar
  • 1,935
3 votes
Accepted

Create an item in two different table with auto-increment C# SQL

I have a couple of comments that might help: I have to make a join with Linq This sounds expensive - are you really pulling all records from all 3 tables into your program just so you can do a ...
Dan Pichelman's user avatar

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