c# - How to store result of a LINQ statement executed in foreach to a variable for future use? -


i need store multiple values satisfies specific condition lstfile. declare:

list<albumphotos> lstfile =  new list<albumphotos>(); 

and used below code id of installed records in t_selectionlistdetails:

idlist = (from s in context.t_selectionlistdetails                           orderby s.id descending                           select new albumphotos { id= s.id }).take(i).tolist(); 

where variable hold how many records saved right now.then need join t_selectionlistdetails table t_useralbumdetails .following code used , return multiple result can't entire result one.

foreach(var item in idlist) {     lstfile = (from s in context.t_selectionlistdetails                join p in context.t_useralbumdetails on s.useralbumdetails_id equals p.id                s.id == item.id                select new albumphotos                        {                            virtualpath = p.virtualpath,                            id = s.id                        }).tolist();            // lstfile.add(t);      }   

how can full result?

you should use addrange add elements of 1 collection end of other list.

foreach(var item in idlist) {     var lstfile1 = (from s in context.t_selectionlistdetails                join p in context.t_useralbumdetails on s.useralbumdetails_id equals p.id                s.id == item.id                select new albumphotos                        {                            virtualpath = p.virtualpath,                            id = s.id                        }).tolist();       lstfile.addrange(lstfile1); }  

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -