AgileRock.com

agile development, asp.net,C#, JSON

Mapping types using ConvertAll ASP.Net 2.0

clock March 24, 2009 11:41 by author tim

A quick way to convert a generic list of one type to another (more for documentation)

List<Merchant> merchant = new List<Merchant>(); 
List<MerchantDTO> mdtos = merchant.ConvertAll<MerchantDTO>(new Converter<Merchant, MerchantDTO>( 
 
     delegate(Merchant m){      
      
      MerchantDTO mdto = new MerchantDTO();
      mdto.ID = m.ID;
      mdto.Name = m.Name; return mdto; 
  
 
      } 
 
)); 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


GenericConverter

clock June 3, 2008 13:27 by author tim

 

using System.Collections.Generic;
using System; 

public class GenericConverter
{ 
     public static T Parse<T>(string sourceValue) where T : IConvertible
     { 

          if (String.IsNullOrEmpty(sourceValue)) return default(T); 
        
         return (T)Convert.ChangeType(sourceValue, typeof(T)); 
     } 

     public static T Parse<T>(string sourceValue, IFormatProvider provider) where T : IConvertible
     { 

          return (T)Convert.ChangeType(sourceValue, typeof(T), provider); 
     } 

} 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Calendar

<<  September 2010  >>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Archive

Tags

Categories


Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in