Linq - formatting a string of two fields in a selected new context

I have an external table with a relationship to one another. I write a linq query as the equivalent of a left join and implement the group by the corresponding id field .

from p in db.personal join pn in
  (from t in db.phoneNumbers
   group t by t.personID into 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 
  into lPN from lpn in lPN.DefaultIfEmpty()
//join wsd in db.basicOperations on p.ID equals wsd.personID
where p.ID == id.Value
select new partialPersonDetailsViewModel()
{
  id = id.Value,
  genderType = p.genderType,
  sPhoneNumbers="(" +lpn.prefix+") "+lpn.number
}).FirstOrDefault();

But at sPhoneNumbers="(" +lpn.prefix+") "+lpn.numberthis place VS will notify me of an error:

Error 1 The + operator cannot be applied to operands like "System.Collections.Generic.IEnumerable" and "System.Collections.Generic.IEnumerable"

Please help me pass this error and solve the problem.

+4
source share
1 answer

Both lpn.prefixand lpn.numberare estimated as g.Select(...), so in fact they are IEnumerable<T>(in the same way as the error message announces it).

g.Select(...).FirstOrDefault() , T IEnumerable<T>.

Update:

, lpn.prefix lpn.number , - - :

sPhoneNumbers = String.Join("; ", 
                            lpn.prefix.Select((p, i) => 
                             String.Format("({0}){1}", 
                                            p, 
                                            lpn.numbers.Skip(i).Take(1).FirstOrDefault())));

Enumerable.Zip, @Chris:

sPhoneNumbers = String.Join("; ", 
                           lpn.prefix.Zip(lpn.numbers, 
                                         (s, s1) => string.Format("({0}){1}", s, s1)));
+3

All Articles