c# - linq - format string from two fields in select new context -
i have foreign table 1 many relation. write linq query left join equivalent , implement group relevant id field.
from p in db.personal join pn in (from t in db.phonenumbers group t t.personid g select new { id = g.key, number = g.select(t => t.number), prefix = g.select(t => t.prefix) }).asenumerable() on p.id equals pn.id lpn lpn in lpn.defaultifempty() //join wsd in db.basicoperations on p.id equals wsd.personid p.id == id.value select new partialpersondetailsviewmodel() { id = id.value, gendertype = p.gendertype, sphonenumbers="(" +lpn.prefix+") "+lpn.number }).firstordefault();
but @ sphonenumbers="(" +lpn.prefix+") "+lpn.number
place vs notify me error:
error 1 operator '+' cannot applied operands of type 'system.collections.generic.ienumerable' , 'system.collections.generic.ienumerable'
please me pass error , solve problem.
both of lpn.prefix
, lpn.number
evaluated g.select(...)
, in fact ienumerable<t>
(exactly error message declares it).
evaluating them g.select(...).firstordefault()
should since extract value of type t
ienumerable<t>
.
update:
in case, when lpn.prefix
, lpn.number
lists of values , need concatenate these lists - can use like:
sphonenumbers = string.join("; ", lpn.prefix.select((p, i) => string.format("({0}){1}", p, lpn.numbers.skip(i).take(1).firstordefault())));
or can use enumerable.zip
method, suggested in comments @chris:
sphonenumbers = string.join("; ", lpn.prefix.zip(lpn.numbers, (s, s1) => string.format("({0}){1}", s, s1)));
Comments
Post a Comment