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
Post a Comment