27

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?

3 Answers 3

55

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))
Sign up to request clarification or add additional context in comments.

3 Comments

with .net 4, you don't need .ToArray().
I need this but for visual Basic .net
@Luis String.Join(",", list.Where(Function(x) x.B).Select(Function(x) x.Txt))
3
string myString = string.Join(",", list.Where(x => x.B).Select(x=>x.Txt));

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.