68225315200a9e0f0b949b7aec9b3b11

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 !

4d72203c38dd5f3e3d2d446b5888e8a7

Elij

July 9, 2008, July 09, 2008 05:20, permalink

3 ratings. Login to rate!

If linq is an option (.net 3.5)

1
2
3
List<Item> items = new List<Item>();

string descriptions = string.Join(",", items.Select(item => item.Description).ToArray());
Avatar

Arjang

July 9, 2008, July 09, 2008 06:30, permalink

No rating. Login to rate!

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)

Cd40128e044f39d7063b5cfdeace80f6

volothamp

July 9, 2008, July 09, 2008 07:25, permalink

1 rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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));
        }
    }
}
861f311cc4a077c439099d0e5d251e73

Mike Minutillo

July 10, 2008, July 10, 2008 05:37, permalink

2 ratings. Login to rate!

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.

Test Harness

1
2
3
4
5
6
7
8
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);
}

Target Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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}
              };
  }
}


Extension Methods

1
2
3
4
5
6
7
8
9
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());
}
861f311cc4a077c439099d0e5d251e73

Wolfbyte

July 10, 2008, July 10, 2008 08:22, permalink

1 rating. Login to rate!

Curses! I wasn't logged in properly for my very first official refactoring. Also, This might look cleaner

1
2
3
4
public static string AsCommaSeparatedValues<T>(this IEnumerable<T> items, Func<T, string> translator)
{
  return items.Select(translator).AsCommaSeparatedValues();
}
3b147eb349748f5a1e50f75e8c4a7867

TheSun

July 14, 2008, July 14, 2008 19:17, permalink

No rating. Login to rate!

.NET 2.0

1
string descriptions = String.Join(",", GetItems().ConvertAll(new Converter<Item, string>(delegate(Item item) { return item.Description; })).ToArray());

Your refactoring





Format Copy from initial code

or Cancel