4

I have a list of objects, Model is:

public class Rule
{
    public bool IsValid { get; set; }
    public string RuleName { get; set; }
    public string Description { get; set; }
}

To get values of RuleName and IsValid from a list of Rules I did the following:

string.Join(", ", list.Select(rule=> new { rule.RuleName, rule.IsValid }))

Current output is in the following format:

{ RuleName = name1, IsValid = True}, { RuleName = name2, IsValid = False }

How to convert it to a format similar to the following without using loops?

name1 is True, name2 is False
2
  • 1
    whats so bad about loops? you could do it with linq but its still internally using loops Commented Feb 20, 2018 at 14:08
  • 2
    You can use string.format inside the select as string.Join(", ", list.Select(rule => string.Format("{0} is {1}", rule.RuleName, rule.IsValid.ToString()))); Commented Feb 20, 2018 at 14:09

1 Answer 1

8

It's simple with String.Join, Enumerable.Select and string interpolation(C#6):

string.Join(", ", list.Select(r=> $"{r.RuleName} is {r.IsValid}"));
Sign up to request clarification or add additional context in comments.

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.