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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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
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#.
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 ...
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 ...
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>();
...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
linq × 88c# × 51
.net × 13
entity-framework × 13
sql × 9
sql-server × 8
algorithms × 6
database × 6
design-patterns × 4
architecture × 4
coding-style × 4
tdd × 4
xml × 4
unit-testing × 3
performance × 3
asp.net × 3
lambda × 3
design × 2
java × 2
object-oriented × 2
object-oriented-design × 2
orm × 2
mvvm × 2
wpf × 2
mocking × 2