从自定义集合创建逗号分隔的字符串(Creating comma separated string from custom collection)

编程入门 行业动态 更新时间:2024-10-28 08:23:12
从自定义集合创建逗号分隔的字符串(Creating comma separated string from custom collection)

我有List<Accrual>和List<Brand> (以及许多其他类似的对象),如下所述。 我需要使用String.Join从List<string>创建一个逗号分隔的字符串

public static string GetCommaSeparatedString(List<string> input) { return String.Join(",", input); } 在List<Accrual>的情况下,我需要将List<Description>传递给方法 在List<Brand>的情况下,我需要将List<Name>传递给方法

我们怎样才能以最易读的方式用最少的代码行来实现它?

注意:我使用的是.Net 4.0

类示例

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; } }

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); } In the case of List<Accrual> I need to pass a List<Description> to the method 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; } }

最满意答案

首先,您可以覆盖ToString方法:

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)); }

其次,您可以使用Linq获取名称,然后加入它们:

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

如果你真的需要泛型方法,那么你可以使用这个(虽然它什么都不给你,至少在这种情况下),这仍然是使用overrriden ToString方法:

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);

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);

更多推荐

本文发布于:2023-08-03 14:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1389397.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   自定义   字符串   Creating   comma

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!