Click here to Skip to main content
15,885,985 members
Home / Discussions / C#
   

C#

 
GeneralRe: GPS Data String Pin
pmarfleet9-Feb-08 7:20
pmarfleet9-Feb-08 7:20 
GeneralRe: GPS Data String Pin
Ed.Poore9-Feb-08 10:25
Ed.Poore9-Feb-08 10:25 
GeneralRe: GPS Data String Pin
Mark Churchill9-Feb-08 14:34
Mark Churchill9-Feb-08 14:34 
QuestionStatic function explained Pin
steve_rm9-Feb-08 2:38
steve_rm9-Feb-08 2:38 
GeneralRe: Static function explained Pin
Not Active9-Feb-08 2:42
mentorNot Active9-Feb-08 2:42 
GeneralRe: Static function explained Pin
DeveshKumar9-Feb-08 5:10
DeveshKumar9-Feb-08 5:10 
GeneralRe: Static function explained Pin
Pete O'Hanlon9-Feb-08 9:29
mvePete O'Hanlon9-Feb-08 9:29 
GeneralLinq query vs foreach loop [modified] Pin
DaveyM699-Feb-08 1:13
professionalDaveyM699-Feb-08 1:13 
I'm loving the Linq stuff, it's very cool and flexible, but I don't want to use is for the sake of it.

I currently have a Generic List of DVD objects that have Name and ID properties.
I'm writing a simple method in the List to return the Name of the DVD given an ID and return an empty string if it's not found.

Before I was doing
public string GetName(int ID)
{
    foreach (DVD thisDVD in this)
    {
        if (thisDVD.ID == ID)
        { return thisDVD.Name; }
    }
    return "";
}

which works fine, but after playing with Linq, I found myself writing
public string GetName(int ID)
{
    var idQuery =
        from dvd in this
        where dvd.ID == ID
        select dvd.Name;
    foreach (string name in idQuery)
    { return name; }
    return "";
}

which works fine too.

I was wondering which to use? I figured that the first one was better, as in the second Linq is iterating over the collection and then I have to iterate over the results. Surely only one trip over and breaking out if/when the required result is found is better? Is there something in the framework that makes Linq more efficient?

Dave

modified on Saturday, February 9, 2008 7:25 AM

GeneralRe: Linq query vs foreach loop Pin
That's Aragon9-Feb-08 1:30
That's Aragon9-Feb-08 1:30 
GeneralRe: Linq query vs foreach loop Pin
Daniel Grunwald9-Feb-08 2:27
Daniel Grunwald9-Feb-08 2:27 
GeneralRe: Linq query vs foreach loop Pin
DaveyM699-Feb-08 3:04
professionalDaveyM699-Feb-08 3:04 
GeneralRe: Linq query vs foreach loop Pin
Gareth H9-Feb-08 1:31
Gareth H9-Feb-08 1:31 
GeneralRe: Linq query vs foreach loop Pin
Daniel Grunwald9-Feb-08 2:36
Daniel Grunwald9-Feb-08 2:36 
GeneralRe: Linq query vs foreach loop Pin
DaveyM699-Feb-08 3:03
professionalDaveyM699-Feb-08 3:03 
GeneralRe: Linq query vs foreach loop Pin
WillemM9-Feb-08 7:54
WillemM9-Feb-08 7:54 
GeneralRe: Linq query vs foreach loop Pin
DaveyM699-Feb-08 8:29
professionalDaveyM699-Feb-08 8:29 
GeneralRe: Linq query vs foreach loop Pin
Guffa9-Feb-08 8:30
Guffa9-Feb-08 8:30 
GeneralRe: Linq query vs foreach loop Pin
DaveyM699-Feb-08 8:44
professionalDaveyM699-Feb-08 8:44 
GeneralRe: Linq query vs foreach loop Pin
Guffa9-Feb-08 11:40
Guffa9-Feb-08 11:40 
GeneralRe: Linq query vs foreach loop Pin
Guffa13-Feb-08 8:19
Guffa13-Feb-08 8:19 
QuestionDocument scanning in C# Pin
Member 26713148-Feb-08 23:27
Member 26713148-Feb-08 23:27 
GeneralRe: Document scanning in C# Pin
Gareth H9-Feb-08 1:33
Gareth H9-Feb-08 1:33 
GeneralRe: Document scanning in C# Pin
Thomas Stockwell9-Feb-08 2:49
professionalThomas Stockwell9-Feb-08 2:49 
GeneralRe: Document scanning in C# Pin
Pete O'Hanlon9-Feb-08 9:27
mvePete O'Hanlon9-Feb-08 9:27 
GeneralRe: Document scanning in C# Pin
Member 267131411-Feb-08 6:44
Member 267131411-Feb-08 6:44 

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.