我有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 methodHow 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);更多推荐
发布评论