c# - oneliner to get biggest something in a list -


i searched on web , can't find if exists.. don't know if question i'll try anyway.

what oneline algorithm retreive biggest in list in c#.

transforming this:

int currentrow = 0;   foreach (customfield cf in fieldlist)     if (cf.row > currentrow)       currentrow = cf.row; 

in this, row of type int. in advance

use linq (enumerable.max):

int currentrow = fieldlist.max(cf => cf.row); 

note: source sequence should not empty. if sequence can empty, , want have default value max (i.e. 0 in sample) project sequence , use enumerable.defaultifempty:

int currentrow = fieldlist.select(cf => cf.row).defaultifempty(0).max(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -