c# - Loop through var from LINQ query? -


my ultimate aim loop through linq query, not sure query doing expecting.

the query below supposed select distinct names sql server db. not throw error, can't test can't seem syntax correct loop through variable.

var staffnames = sql.staff_time_tbls.select(item => item.staff_name).distinct(); 

my loop below, not correct. getting red underline on both staffnames.lenght , staffnames[i]

            (int = 0; < staffnames.lenght; i++)             {                 messagebox.show(staffnames[i]);             } 

i tried test if results query correct no luck. here item

             foreach (var item in staffnames)             {                 messagebox.show(item);             } 

enter image description here

changed still error,

enter image description here

simple foreach trick,

var staffnames = sql.staff_time_tbls.select(item => item.staff_name).distinct();  foreach(var name in staffnames) {    messagebox.show(name.tostring()); } 

or:

var staffnames = sql.staff_time_tbls.select(item => item.staff_name).distinct().tolist();  (int = 0; < staffnames.count; i++) {      messagebox.show(staffnames[i].tostring()); } 

things note, staffnames iqueryable , doesn't hold data unless enumerate it (loop through it). that's why don't have length property or indexer [i]. however, iqueryable implements ienumerable. can iterate through using foreach. when start iterating on (using foreach or tolist(), the database call done.