class Item
{
int Id{get;set;}
string Description{get;set;}
}
private string CommaString(List<Item> Items)
{
if (Items.Count==0) return "";
string[] strings = new string[Items.Count];
for( int i=0 ; i<Items.Count ;i++ )
{
strings[i]=Items[i].Description;
}
return string.Join(",", strings);
}
Refactorings
No refactoring yet !
Elij
July 9, 2008, July 09, 2008 05:20, permalink
If linq is an option (.net 3.5)
List<Item> items = new List<Item>();
string descriptions = string.Join(",", items.Select(item => item.Description).ToArray());
Arjang
July 9, 2008, July 09, 2008 06:30, permalink
LINQ is definitely an option! Damn, it almost looks like a cheat, so concise and compact :)
Thank you Eliji
Now only from theoretical point of view I wonder about using an extension method on a list with a call similar (or more generic ) than :
string descriptions= GetItems().CommaString(Item.Description)
volothamp
July 9, 2008, July 09, 2008 07:25, permalink
using System;
using System.Collections.Generic;
using System.Linq;
namespace Foldr {
internal class Program {
private static void Main(string[] args) {
var s1 = Item.GetItems().CommaString();
var s2 = Item.GetItems().CommaStringBis();
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.ReadKey();
}
}
internal class Item {
public int Id { get; set; }
public string Description { get; set; }
public static List<Item> GetItems() {
return new List<Item>
{
new Item {Description = "Pippo", Id = 2},
new Item {Description = "Paperino", Id = 2},
new Item {Description = "Pluto", Id = 2}
};
}
}
internal static class Helpers {
public static string CommaString(this List<Item> items) {
return String.Join(",", items.Select(i => i.Description).ToArray());
}
public static string CommaStringBis(this List<Item> items) {
return items.Select(i => i.Description).Aggregate((s1, s2) => String.Format("{0},{1}",s1, s2));
}
}
}
Mike Minutillo
July 10, 2008, July 10, 2008 05:37, permalink
I'd consider making it more generic by separating out the code that extracts the desired string. Now you can use it to target anything and produce Comma Separated strings from any IEnumerable<T> out there. The overloaded calling convention means that if you have strings already you can just get them as a comma-separated string and if you don't already have strings you can supply a lambda which produces a string for each item in your collection.
static void Main(string[] args)
{
var descriptions = Item.GetItems().AsCommaSeparatedValues(i => i.Description);
var ids = Item.GetItems().AsCommaSeparatedValues(i => i.Id.ToString());
Console.WriteLine(descriptions);
Console.WriteLine(ids);
}
class Item
{
public int Id { get; set; }
public string Description { get; set; }
public static List<Item> GetItems()
{
return new List<Item>
{
new Item {Description = "Pippo", Id = 1},
new Item {Description = "Paperino", Id = 2},
new Item {Description = "Pluto", Id = 3}
};
}
}
public static string AsCommaSeparatedValues<T>(this IEnumerable<T> items, Func<T, string> translator)
{
return AsCommaSeparatedValues(items.Select(translator));
}
public static string AsCommaSeparatedValues( this IEnumerable<string> strings )
{
return String.Join(",", strings.ToArray());
}
Wolfbyte
July 10, 2008, July 10, 2008 08:22, permalink
Curses! I wasn't logged in properly for my very first official refactoring. Also, This might look cleaner
public static string AsCommaSeparatedValues<T>(this IEnumerable<T> items, Func<T, string> translator)
{
return items.Select(translator).AsCommaSeparatedValues();
}
TheSun
July 14, 2008, July 14, 2008 19:17, permalink
.NET 2.0
string descriptions = String.Join(",", GetItems().ConvertAll(new Converter<Item, string>(delegate(Item item) { return item.Description; })).ToArray());
I like to to traverse a list and give the property on the list ( not the name of property but actually the property description itself) and get back a comma separated string I have send the code I am using now but ideally instead of using following call:
string descriptions= CommaStrings( GetItems() );
use the follwing call:
string descriptions= GetItems().CommaString(Item.Description);
If possible.
Thanks for any ideas, suggestion, criticisms.
Regards
Arjang