Common list.

I have an object

public class Title
    {
        public int Id {get; set; }
        public string Title {get; set; }
    }

How to join the whole title with a "-" in List<Title>?

+3
source share
2 answers

I think this should give you what you are looking for. This will allow you to select the Title property from each object into an array of strings, and then join all the elements of this array into a string separated by "-".

List<Title> lst = new List<Title>
                    { 
                        new Title{Id = 1, Title = "title1"}, 
                        new Title{Id = 2, Title = "title2"} 
                    }
String.Join("-", lst.Select(x => x.Title).ToArray());

If you are using .NET 4.0 or later, there is now an overload to String.Join, which allows you to omit .ToArray():

String.Join("-", lst.Select(x => x.Title));
+11
source
list.Select(x => x.Title).Aggregate((current, next) => current + "-" + next);

should return a chain of them all chains using <->

+2
source

Source: https://habr.com/ru/post/1782481/


All Articles