Click here to Skip to main content
15,899,935 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Using IEnumerable nonsense for everything Pin
Maarten197718-Jul-16 3:32
Maarten197718-Jul-16 3:32 
GeneralRe: Using IEnumerable nonsense for everything Pin
Clifford Nelson16-Jul-16 18:40
Clifford Nelson16-Jul-16 18:40 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore17-Jul-16 2:40
professionalF-ES Sitecore17-Jul-16 2:40 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming18-Jul-16 3:23
mveRichard Deeming18-Jul-16 3:23 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore18-Jul-16 3:35
professionalF-ES Sitecore18-Jul-16 3:35 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming18-Jul-16 3:44
mveRichard Deeming18-Jul-16 3:44 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore18-Jul-16 3:47
professionalF-ES Sitecore18-Jul-16 3:47 
GeneralRe: Using IEnumerable nonsense for everything PinPopular
Richard Deeming18-Jul-16 4:08
mveRichard Deeming18-Jul-16 4:08 
As others have pointed out, ForEach is the odd man out here. A foreach loop of the results of the sequence returned from LINQ is the better option.

But the bulk of LINQ is about telling the compiler what to do, not how to do it. And that makes the code much more readable (for some).


Imagine you start with a big block of code in a single method. It takes a list, filters it, sorts it, groups it, filters it some more, projects it, and then processes it. You've got a fairly complex method which is specific to one task. If you need to repeat any of those operations, you have to duplicate the code.

The first thing you would do is refactor the code, to move some of the common operations out into separate, simpler, reusable methods. You could then write simple unit tests for those methods, without having to set up the more complicated data for your original method, and without having to work out which part of the original method failed if the tests failed.

Then, you would reuse those simpler methods elsewhere when you needed to do the same thing. Need to filter a list? Call SomeClass.FilterAList. Need to group a list? Call SomeClass.MakeSomeGroups.

Pretty soon, you end up with a collection of utility methods that you're reusing everywhere. But the syntax is quite nasty:
C#
var source = GetAList();
var filtered = SomeClass.FilterAList(source, SomeFilter);
var sorted = SomeClass.MakeItSorted(filtered, SomeSortingCondition);
var grouped = SomeClass.MakeSomeGroups(sorted, SomeGroupingCondition);
var filteredAgain = SomeClass.FilterAList(grouped, AnotherFilter);
var result = SomeClass.ProjectAList(filteredAgain, SomeProjection);

// Or:
var result = SomeClass.ProjectAList(
    SomeClass.FilterAList(
        SomeClass.MakeSomeGroups(
            SomeClass.MakeItSorted(
                SomeClass.FilterAList(
                    GetAList(), 
                    SomeFilter), 
                SomeSortingCondition), 
            SomeGroupingCondition), 
        AnotherFilter), 
    SomeProjection);

To tidy it up, you would like to be able to call each utility method as if it was defined on the IEnumerable<T> interface. You can't add the methods to the interface, since that would break everything that implemented it. So instead, you introduce extension methods, and the syntax becomes:
C#
var result = GetAList()
    .FilterAList(SomeFilter)
    .MakeItSorted(SomeSortingCondition)
    .MakeSomeGroups(SomeGroupingCondition)
    .FilterAList(AnotherFilter)
    .ProjectAList(SomeProjection);

Now it's much easier to see what's going on, which condition applies to which operation, etc.

Change the method names, and you've effectively reinvented LINQ. Smile | :)


F-ES Sitecore wrote:
You also forget "harder to debug" Smile | :)

Only if you're trying to debug the framework code. If you stick to debugging your own code, it's easier to debug, because there's less of it. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore18-Jul-16 4:51
professionalF-ES Sitecore18-Jul-16 4:51 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming18-Jul-16 4:54
mveRichard Deeming18-Jul-16 4:54 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore18-Jul-16 22:28
professionalF-ES Sitecore18-Jul-16 22:28 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming19-Jul-16 1:17
mveRichard Deeming19-Jul-16 1:17 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore19-Jul-16 2:38
professionalF-ES Sitecore19-Jul-16 2:38 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming19-Jul-16 2:40
mveRichard Deeming19-Jul-16 2:40 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore19-Jul-16 2:50
professionalF-ES Sitecore19-Jul-16 2:50 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming19-Jul-16 2:54
mveRichard Deeming19-Jul-16 2:54 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore19-Jul-16 3:17
professionalF-ES Sitecore19-Jul-16 3:17 
GeneralRe: Using IEnumerable nonsense for everything Pin
Richard Deeming19-Jul-16 3:21
mveRichard Deeming19-Jul-16 3:21 
GeneralRe: Using IEnumerable nonsense for everything Pin
F-ES Sitecore19-Jul-16 3:26
professionalF-ES Sitecore19-Jul-16 3:26 
GeneralRe: Using IEnumerable nonsense for everything Pin
James Curran18-Jul-16 4:26
James Curran18-Jul-16 4:26 
GeneralRe: Using IEnumerable nonsense for everything Pin
Mike Marynowski18-Jul-16 7:31
professionalMike Marynowski18-Jul-16 7:31 
GeneralRe: Using IEnumerable nonsense for everything Pin
James Curran18-Jul-16 8:10
James Curran18-Jul-16 8:10 
GeneralRe: Using IEnumerable nonsense for everything Pin
Mike Marynowski18-Jul-16 8:16
professionalMike Marynowski18-Jul-16 8:16 
GeneralRe: Using IEnumerable nonsense for everything Pin
James Curran19-Jul-16 4:00
James Curran19-Jul-16 4:00 
GeneralRe: Using IEnumerable nonsense for everything Pin
Mike Marynowski19-Jul-16 7:24
professionalMike Marynowski19-Jul-16 7:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   479 votes