Search code examples
linqc#-4.0lambda

Concatenate string properties of an object with lambda


Please consider the following:

public class MyObject
{
   public bool B;
   public string Txt;
}

List<MyObject> list; //list of a bunch of MyObject's 

With lambda expression, how can I produce a string consisting of comma separated values of Txt of those objects, where B is true?


Solution

  • for .net 3.5:

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

    for .net 4.0:

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