Thursday, 15 January 2015

asp.net mvc 3 - How to aggregate string using Linq 2 Entities -



asp.net mvc 3 - How to aggregate string using Linq 2 Entities -

i have iqueryable<model> of objects in db. need cast them iqueryable<viewmodel>

class model { public guid modelid { get; set; } // properties public virtual user users{ get; set; } } class user { public guid userid { get; set; } public string firstname { get; set; } public string lastname { get; set; } } class viewmodel { public guid modelid { get; set } public string usersname { get; set; } }

viewmodel.usersname should merged string of users first , lastly names. need iqueryable since i'll utilize devexpress mvc grid , i'll need pass in way. grid handles paging, needs iqueryable.

i've tried:

iqueryable<viewmodel> viewmodel = model.select(s => new viewmodel() { usersname = s.users .orderby(d => d.lastname) .thenby(d => d.firstname) .aggregate(string.empty, (s1, users) => users.firstname + ' ' + users.surname + ", ") });

but it's not translatable linq entities. i've tried string.join() won't work neither.

the result string should expample : "cecelia leblanc, sarah hodge" ... etc.

thanks input.

usually view models/dtos should not smart, case little bit of smartness comes in handy comfort. create view model like:

class viewmodel { public guid modelid { get; set; } public ienumerable<string> usernames { get; set; } public string usernamesstring { { homecoming string.join(", ", this.usernames); } } }

populate by

var models = s.models.select(m => new viewmodel { modelid = m.modelid, usernames = m.users .orderby(d => d.lastname) .thenby(d => d.firstname) .select(u => u.firstname + ' ' + u.surname) }).tolist();

and access usernamesstring property models.asqueryable().

asp.net-mvc-3 linq-to-entities devexpress

No comments:

Post a Comment