简体   繁体   中英

Creating comma separated string from custom collection

I have List<Accrual> and List<Brand> (and many other similar objects) as mentioned below. I need to create a comma separated string from a List<string> using String.Join

    public static string GetCommaSeparatedString(List<string> input)
    {
        return String.Join(",", input);
    }
  1. In the case of List<Accrual> I need to pass a List<Description> to the method
  2. In the case of List<Brand> I need to pass a List<Name> to the method

How can we achieve it in the most readable way with least number of lines of code?

Note: I am using .Net 4.0

Class Examples

public class Accrual
{
    public string Code { get; set; }
    public string Description { get; set; }

}

public class Brand
{
    public int Number { get; set; }
    public string Name { get; set; }
}

Firstly, you can override ToString method:

public class Brand
{
    public int Number { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

void Method()
{
    var brands = new List<Brand>()
    {
        new Brand { Number = 1, Name = "a" },
        new Brand { Number = 2, Name = "b" }
    };
    // outputs: a,b
    Console.WriteLine(string.Join(",", brands));
}

Secondly, you can use Linq to get names, and then join them:

var brandsNames = brands.Select(i => i.Name);
string joinedNames = string.Join(",", brandsNames);

If you really need generic method for that then you can use this one (although it gives you nothing, at least in this case), this is still using overrriden ToString method:

public static class Formatter
{
    public static string GetCommaSeparatedString<T>(IEnumerable<T> input)
    {
        return string.Join(",", input);
    }
}
// and then
string brandsStrings = Formatter.GetCommaSeparatedString<Brand>(brands);
// or just
string brandsStrings = Formatter.GetCommaSeparatedString(brands);

Well, first I'd change GetCommaSeparatedString method signature to support IEnumerable<string> instead of List<string> and turn it into an extension method:

public static string GetCommaSeparatedString(this IEnumerable<string> input)
{
    return String.Join(",", input);
}

Then simply do:

var accDescrs = listAccruals.Select(x => x.Description).GetCommaSeparatedString();
var brndNames = listBrands.Select(x => x.Name).GetCommaSeparatedString();
public static string SeparateByCommas<T, TProp>(this IEnumerable<T> source, Expression<Func<T, TProp>> expression)
{
    var memberExpression = expression.Body as MemberExpression;
    if (memberExpression == null)
        return string.Empty;

    var propName = memberExpression.Member.Name;
    return source.Aggregate(new StringBuilder(), (builder, t) =>
           {
               builder.Append(typeof (T).GetProperty(propName).GetValue(t).ToString());
               builder.Append(",");
               return builder;
           }).ToString();
}

var test = new List<Brand>
{
   new Brand
   {
     Number = 1,
     Name = "a"
   },
   new Brand
   {
     Number = 2,
     Name = "b"
   }
};


string separateByCommas = test.SeparateByCommas(brand => brand.Name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM