用lambda连接对象的字符串属性



请考虑以下事项:

public class MyObject
{
   public bool B;
   public string Txt;
}
List<MyObject> list; //list of a bunch of MyObject's 

使用lambda表达式,我如何生成一个由逗号分隔的Txt值组成的字符串,其中B为真?

for .net 3.5:

string.Join(",", list.Where(o => o.B).Select(o => o.Txt).ToArray())

:

string.Join(",", list.Where(o => o.B).Select(o => o.Txt))
string myString = string.Join(",", list.Where(x => x.B).Select(x=>x.Txt));
var strFinal = "";
myList.ForEach(pq => strFinal += ", " + pq.ItemId);

相关内容

  • 没有找到相关文章

最新更新