Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert values of List objects to string

Tags:

c#

linq

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
like image 307
Yahya Hussein Avatar asked Jan 29 '23 15:01

Yahya Hussein


1 Answers

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

string.Join(", ", list.Select(r=> $"{r.RuleName} is {r.IsValid}"));
like image 99
Tim Schmelter Avatar answered Feb 06 '23 12:02

Tim Schmelter