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,093
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
  • 7,727
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,524
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.5k
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
  • 33.7k
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
  • 206k
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.3k
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
  • 33.7k
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.4k
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

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
  • 10k
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
  • 206k
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
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,508
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.4k
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

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.7k
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.2k
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
  • 33.7k
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
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
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

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