将文本合并到linq Select语句中?

时间:2018-10-02 19:28:34

标签: c# linq

我有一堂这样的课

 public class ItemImage
    {
        public string Name { get; set; }

    }

现在我想将url部分附加到名称中。

//结果将为http://www.example.com/ {名称}

我可以选择

images.Select(x => x.Name )

但是我不确定如何一次性添加url +名称。

我尝试过

images.Select(x => new { fullImagePath= "http://www.example.com/" + x.Name })

但是我只想要一个简单的数组(即[“ http://www.example.com/1.jpg”,“ http://www.example.com/2.jpg”]

2 个答案:

答案 0 :(得分:1)

您可以使用string interpolation

<table>
  <tr>
    <th rowspan="2" id="h">Homework</th>
    <th colspan="3" id="e">Exams</th>
    <th colspan="3" id="p">Projects</th>
  </tr>
  <tr>
    <th id="e1" headers="e">1</th>
    <th id="e2" headers="e">2</th>
    <th id="ef" headers="e">Final</th>
    <th id="p1" headers="p">1</th>
    <th id="p2" headers="p">2</th>
    <th id="pf" headers="p">Final</th>
  </tr>
  <tr>
    <td headers="h">15%</td>
    <td headers="e e1">15%</td>
    <td headers="e e2">15%</td>
    <td headers="e ef">20%</td>
    <td headers="p p1">10%</td>
    <td headers="p p2">10%</td>
    <td headers="p pf">15%</td>
  </tr>
</table>

示例:

images.Select(x => $"http://www.example.com/{x.Name}" )

答案 1 :(得分:1)

在C#之前的版本5

images.Select(x => string.format("http://www.example.com/{0}", x.Name))

在C#6 +

images.Select(x => $"http://www.example.com/{x.Name}" )
相关问题