Asp.net 如何将Linq.IEnumerable(字符串的)转换为格式化字符串
我有一个存储验证错误的Asp.net 如何将Linq.IEnumerable(字符串的)转换为格式化字符串,asp.net,.net,asp.net-4.5,Asp.net,.net,Asp.net 4.5,我有一个存储验证错误的列表(字符串)。如果该列表包含任何项目,我想将它们连接到一个HTML列表中以显示每个错误。目前,这样做很容易: Dim l As List(Of String) = GetErrors() If l.Count > 0 Then Dim sb As New StringBuilder sb.Append("<div><ul>") For Each s As String In l sb.Append(St
列表(字符串)
。如果该列表包含任何项目,我想将它们连接到一个HTML列表中以显示每个错误。目前,这样做很容易:
Dim l As List(Of String) = GetErrors()
If l.Count > 0 Then
Dim sb As New StringBuilder
sb.Append("<div><ul>")
For Each s As String In l
sb.Append(String.Format("<li>{0}</li>", s))
Next
sb.Append("</ul></div>")
ltl_status.Text = sb.ToString()
End If
这里的目标是使用尽可能少的代码行构建列表。我看到String.Join
接受IEnumerable参数,但它只是将项目连接在一起,而这里我需要在每个项目的开头和结尾添加额外的字符串。可能吗
答复
基于Jon Skeet的优秀建议,扩展方法为我节省了大量的时间和精力:
Public Module CollectionSignatureMethods
''' <summary>
''' Takes each String value in a String collection, reformats using a format, and then returns all as a single String.
''' </summary>
''' <param name="ie">An IEnumerable(Of String) collection of string values</param>
''' <param name="formatString">The string format (as per String.Format)</param>
''' <returns>All of the Strings from the collection, reformatted and combined to a single String</returns>
''' <remarks>Jon Skeet is the daddy(!)</remarks>
<Extension()> _
Public Function JoinFormat(ie As IEnumerable(Of String), formatString As String) As String
Return String.Join(String.Empty, ie.Select(Function(s As String) String.Format(formatString, s)))
End Function
End Module
公共模块集合签名方法
'''
''获取字符串集合中的每个字符串值,使用格式重新格式化,然后将所有值作为单个字符串返回。
'''
''字符串值的IEnumerable(字符串)集合
''字符串格式(根据string.format)
''集合中的所有字符串,重新格式化并组合为单个字符串
''乔恩·斯基特是爸爸(!)
_
公共函数JoinFormat(即作为IEnumerable(字符串的),formatString作为字符串)作为字符串
返回String.Join(String.Empty,ie.Select(函数作为字符串)String.Format(formatString,s)))
端函数
端模块
听起来您需要将Join
和Select
结合起来:
String.Join("", _
l.Select(Function(s as String) String.Format("<li>{0}</li>", s))
太好了,谢谢。您建议将扩展方法添加到哪个类?由于IEnumerable等其他类型可能也会从中受益,因此(字符串的)列表在层次结构中的位置可能太高it@EvilDr:我个人认为
IEnumerable(字符串)
。或者甚至IEnumerable(Of T)
,如果您愿意对任何值都采用默认格式。我更新了这个问题,希望它能在将来帮助其他人。
Public Module CollectionSignatureMethods
''' <summary>
''' Takes each String value in a String collection, reformats using a format, and then returns all as a single String.
''' </summary>
''' <param name="ie">An IEnumerable(Of String) collection of string values</param>
''' <param name="formatString">The string format (as per String.Format)</param>
''' <returns>All of the Strings from the collection, reformatted and combined to a single String</returns>
''' <remarks>Jon Skeet is the daddy(!)</remarks>
<Extension()> _
Public Function JoinFormat(ie As IEnumerable(Of String), formatString As String) As String
Return String.Join(String.Empty, ie.Select(Function(s As String) String.Format(formatString, s)))
End Function
End Module
String.Join("", _
l.Select(Function(s as String) String.Format("<li>{0}</li>", s))
ltl_status.Text = String.Format("<div class=""failure""><ul>{0}</ul></div>",
l.JoinFormat("<li>{0}</li>"));