English 中文(简体)
是否有可能利用伊斯兰堡的功能而不是 for子建造一个护堤? [复制]
原标题:Is it possible to construct a stringbuilder using a lambda function instead of foreach? [duplicate]
  • 时间:2010-02-02 19:09:47
  •  标签:
  • linq
  • lambda

This question already has answers here:

Closed 13 years ago.

Possible Duplicate:
LINQ to append to a StringBuilder from a String[]

放弃我的职能性方案,但甚至有可能利用灯巴功能,在阵列中将每一条扼杀都传给一个斯特林物体?

有可能修改这一法典:


// string[] errors = ...
StringBuilder sb = new StringBuilder("<ul>");

foreach (var error in errors)
{
    sb.AppendFormat("<li>{0}</li>", error);
}

sb.AppendLine("</ul");
return sb.ToString();


类似情况:


// string[] errors = ...
StringBuilder sb = new StringBuilder("<ul>");

//I know this isn t right, I don t care about a return value
errors.All(s => sb.AppendFormat("<li>{0}</li>", s));

sb.AppendLine("</ul");
return sb.ToString();


感谢任何音响!

最佳回答

我先对它进行了测试,但像以下这样的东西应当奏效。

return errors.Aggregate(new StringBuilder("<ul>"), (sb,s) => sb.AppendFormat("<li>{0}</li>", s))
             .Append("</ul>");
             .ToString();

汇总是一种可观的延伸方法...... 但是,我可能会有错觉。 我个人认为,这不会给你带来很多好处,因为它不易读,而是在内部进行<条码>。

问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签