简体   繁体   中英

C# Lambda Expression

I am trying to copy values from an IList collection(Entities here) to stringbuilder as

Entites.Select(n => objStringBuilder.AppendLine(n.Note));

Can anyone please guide what am I doing wrong here?

Thank you!

That looks more like a foreach operation. i'd stick with the traditional for each in this case:

foreach (var n in Entities) { 
    objStringBuilder.AppendLine(n.Note);
 }

Select is not what you want to use you here. Try casting to a List<T> and using ForEach instead.

Entities.ToList().ForEach(n => objStringBuilder.AppendLine(n.Note));
String.Join("", entities.Select(x => x.Note).ToArray())

or if you insist on StringBuilder:

Entities.Foreach(x => sb.Append(x.Note))

(The Select expects that you provide a returned value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM