c# - Get multiple fields in an array linq -


in application user can have multiple locations. want show locations of user in dropdown. created model having 2 fields userid , locations. locations array of strings. want linq query userid , locations. possible in linq?

public partial class modellocation {     public int userid { get; set; }           public string[] locations{ get; set; } } 

in database records like

userid   location 1           1           b 2           3           b 3           c 3           d 

var models = db.locations.groupby(l => l.userid) .select(l => new modellocation()  {    userid = l.key,    locations = l.select(l => l.location).toarray()  }); 

depending on linq engine using (linq-to-sql, linq-to-entities) query can bad performance , result in multiple queries being sent db. way solve retrieve data db first , perform grouping in memory instead introducing call asenumerable().

var models = db.locations.asenumerable().groupby(l => l.userid) .select(l => new modellocation()  {    userid = l.key,    locations = l.select(l => l.location).toarray()  }); 

if want filtering where() should before call asenumerable() filtering done in db.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -