I need to submit a list of complete addresses to BingMaps for directions. My table has separate columns for Address and Zip Code.
Example Address: 1 State Street New York NY Example Zip Code: 10001
I need each entry of the list to have a space or comma. How can I achieve this with Linq?
One variation I was trying:
var iFou = _context.PostFous
.Where(m => m.FouZero == zero && m.FouAddr != null)
.Select(p => new { p.FouAddr, " ", p.FouPost })
.ToList();
SOLUTION:
List<string> iFou = _context.PostFous
.Where(m => m.FouZero == zero && m.FouAddr != null)
.Select(p => $"{p.FouAddr} {" "} {p.FouPost}")
.ToList();
CodePudding user response:
I don't know why you select an anonymous type if you just need a single string.
List<string> addressList = _context.PostFous
.Where(m => m.FouZero == zero && m.FouAddr != null)
.Select(p => $"{p.FouAddr} {p.FouPost}")
.ToList();
CodePudding user response:
Try this. string.Join()
will concatenate your array of strings, with your choice of separator in between. More on this here: https://learn.microsoft.com/en-us/dotnet/api/system.string.join
var iFou = _context.PostFous
.Where(m => m.FouZero == zero && m.FouAddr != null)
.Select(p => string.Join(' ', new [] {p.FouAddr, p.FouPost /* etc */})
.ToList();