我有以下带有简单字符串插值的代码:
public string CreateStyledGuestList(VolunteerDTO volunteerData)
{
List<GuestContact> guestListWithLeaderIncluded = GetNewGuestListWithLeaderIncluded(volunteerData);
string styledGuestList =
"<div style=\"margin-left: 40px\">"
+ "<br>" + guestListWithLeaderIncluded.Select(g => g.FirstName + " " + g.LastName)
+ "</div>";
return styledGuestList;
}
测试此方法时,我得到以下信息(请注意,显示的是代码,而不是结果的字符串):
↓ (pos 35)
预计:···杜松子酒:40px“>
Leeroy Jenkins 实际:···左金边:40px“>
System.Linq.Enumerable + SelectListIterator··· ↑(pos 35)
测试本身:
[Fact]
public void ShouldCreateGuestListWithOnlyLeader()
{
VolunteerDTO volunteerData = new VolunteerDTO();
volunteerData.FirstName = "Leeroy";
volunteerData.LastName = "Jenkins";
volunteerData.Guests = new List<GuestContact>();
string actual = _fixture.CreateStyledGuestList(volunteerData);
string expected = "<div style=\"margin-left: 40px\"><br>Leeroy Jenkins</div>";
Assert.Equal(expected, actual);
}
为什么不对字符串进行插值/如何解决?
答案 0 :(得分:2)
guestListWithLeaderIncluded.Select(...)
将返回IEnumerable<string>
而不是string
您需要将字符串连接到一个字符串,例如:
... + string.Join("<br>", guestListWithLeaderIncluded.Select(...)) + ...