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?

Thank you.

Answers


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))

string myString = string.Join(",", list.Where(x => x.B).Select(x=>x.Txt));

var strFinal = "";
myList.ForEach(pq => strFinal += ", " + pq.ItemId);

Need Your Help

NumPy array is not JSON serializable

python json django numpy

After creating a NumPy array, and saving it as a Django context variable, I receive the following error when loading the webpage:

Exceptions and error codes: mixing them the right way

c++ exception error-handling

I am developing a C++ dongle communication library. The library would provide an unified interface to communicate with a range of remote code execution dongles like SenseLock, KEYLOK, Guardant Code...