Enumerable.Except
-
More reason not to use Linq.
Nah. LINQ saves us from so many headaches and ugly procedural code. We really love it here. It's just that there's some gotchas to watch out for.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
-
A coworker found this rather surprising aspect of the
Enumerable.Except
LINQ extension method:var items = new [] { 10, 10 };
var result = items.Except(42);What would you expect the result to be? We expected result to be a sequence of {10, 10}. Surprise! It's { 10 };
Except
apparently removes all non-distinct items from the sequence, in addition to the elements passed in. X|Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
Hey Judah, Except returns the set difference, which is well documented, so I don't see how this is surprising. I think your coworker just did not understand what the method does.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
More reason not to use Linq.
PIEBALDconsult wrote:
More reason not to use Linq.
What would the other reasons be?
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
Nah. LINQ saves us from so many headaches and ugly procedural code. We really love it here. It's just that there's some gotchas to watch out for.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
Judah Himango wrote:
It's just that there's some gotchas to watch out for.
One mistake I've made in the past that I've seen other people make too is a tendency to overuse it without thinking of performance or whether there are simpler ways to do it. Like people blindly calling ToList, ToArray etc. Just because it's so easy and doable does not mean you should. On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist :-D
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
Hey Judah, Except returns the set difference, which is well documented, so I don't see how this is surprising. I think your coworker just did not understand what the method does.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkYeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:
public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
{
foreach(var item in sequence)
{
if (!items.Contains(item)) yield return item;
}
}Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
-
Judah Himango wrote:
It's just that there's some gotchas to watch out for.
One mistake I've made in the past that I've seen other people make too is a tendency to overuse it without thinking of performance or whether there are simpler ways to do it. Like people blindly calling ToList, ToArray etc. Just because it's so easy and doable does not mean you should. On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist :-D
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkNishant Sivakumar wrote:
On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist
:laugh: I didn't know I was the LINQ evangelist of CP. :cool::thumbsup: But I just know the basics; I discovered the "into" keyword just the other day, for example. So I'm basically a newb.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
-
More reason not to use Linq.
PIEBALDconsult wrote:
More reason not to use Linq
1.00/5 (4 votes) A popular opinion, isn't it?
-
PIEBALDconsult wrote:
More reason not to use Linq
1.00/5 (4 votes) A popular opinion, isn't it?
Obviously I don't say things for popularity. :rolleyes:
-
More reason not to use Linq.
Well, if you know how it works, then you have all the reasons to use it. Linq to SQL is another case though. If you work with more than a handful of records/objects at a time, the performance is just horrible. I had to rewrite a lot of code because of its inefficiencies. In some cases, the new and improved code was 70 times(!) faster.
-- Kein Mitleid Für Die Mehrheit
-
Yeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:
public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
{
foreach(var item in sequence)
{
if (!items.Contains(item)) yield return item;
}
}Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
I agree. It ought to have been named SetDifference() or maybe just Difference().
-- Kein Mitleid Für Die Mehrheit
-
Yeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:
public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
{
foreach(var item in sequence)
{
if (!items.Contains(item)) yield return item;
}
}Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
Maybe you should add an extension method named Expect then :D