Search code examples
c#linq

LINQ Select 2 columns, add string, and add to list


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();

Solution

  • 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();