Click here to Skip to main content
15,919,245 members
Home / Discussions / C#
   

C#

 
GeneralWhich patterns are the best choice for business layer Pin
coolcoder9-Feb-08 8:04
coolcoder9-Feb-08 8:04 
GeneralRe: Which patterns are the best choice for business layer Pin
PIEBALDconsult9-Feb-08 10:17
mvePIEBALDconsult9-Feb-08 10:17 
GeneralRe: Which patterns are the best choice for business layer Pin
Ed.Poore9-Feb-08 10:22
Ed.Poore9-Feb-08 10:22 
QuestionHow to render html in C# window application [modified] Pin
WinSolution9-Feb-08 7:07
WinSolution9-Feb-08 7:07 
GeneralRe: How to render html in C# window application Pin
Pete O'Hanlon9-Feb-08 9:30
mvePete O'Hanlon9-Feb-08 9:30 
GeneralCustom Event Handler Problem. Pin
hdv2129-Feb-08 6:51
hdv2129-Feb-08 6:51 
GeneralRe: Custom Event Handler Problem. Pin
Gareth H9-Feb-08 7:22
Gareth H9-Feb-08 7:22 
GeneralRe: Custom Event Handler Problem. Pin
amargujrathi20069-Feb-08 8:06
amargujrathi20069-Feb-08 8:06 
GeneralRe: Custom Event Handler Problem. Pin
hdv2129-Feb-08 8:10
hdv2129-Feb-08 8:10 
GeneralGPS Data String Pin
Simerjots9-Feb-08 5:29
Simerjots9-Feb-08 5:29 
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 

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.