c# - how the below cases execute and how can i do it more efficient into the memory and performance related? -


case 1:

var lstusers = myprogram.getusres();  if(lstusers.any()){     string existinguser = lstuser.first().username; } 

case 2:

if(myprogram.getusres().where(qry => qry.username.lower("xyz").any()){     string existinguser = myprogram.getusres().where(qry => qry.username.lower("xyz")).first().username; } 

please me understand case can take more time , memory run above code?

why?

it's going entirely dependant on implementation of myprogram.getusres();

if function expensive case 2 slower function called twice. being said depends myprogram.getusres() does. if returns ienumerable() expensive calculate advantage disappears.

an alternative process once

var username = myprogram.getusers()                         .select(x => x.username)                         .firstordefault(x => x.username.lower("xyz")); 

username either first match or null on no match.

this assuming lower function returns bool, i'm assuming x.username.tolower() =="xyz", if want.

var username = myprogram.getusers()                         .select(x => x.username)                         .firstordefault(x =>                                string.compare(x,                                              "xyz",                                              stringcomparison.ordinalignorecase) == 0); 

Comments