2009-11-18 8 views
7

Potrzebowałem "obrócić" lub "obrócić" zbiór obiektów. Przykładem będzie wyjaśnić to, co potrzebne do zrobienia:Jak się przestawić IZlicznie <T>

var people = new List<Person>(new[] { 
       new Person{ Name="Ronnie",Age=25, HairColor="Brown",EyeColor="Blue"}, 
       new Person{ Name="Tina",Age=25, HairColor="Brown",EyeColor="Green"}, 
       new Person{ Name="Lukus",Age=4, HairColor="Blonde",EyeColor="Blue"}  
      }); 

DataTable rotatedData = people.Pivot("Name", "Property");     

/* Results In: 
* ___________________________________________ 
* Property | Ronnie | Tina | Lukus 
* ___________________________________________ 
* Age  | 25  | 25  | 4 
* HairColor | Brown | Brown | Blonde 
* EyeColor | Blue | Green | Blue 
* ____________________________________________ 
*/ 

Oto kod, który napisałem to osiągnąć:

/// <summary>Converts an IEnumerable into a pivoted DataTable object.</summary> 
/// <param name="collection">The IEnumerable object to pivot and convert.</param> 
/// <param name="headingMember">The name of a public field or property member of type T to be used as column name's in the pivoted DataTable object.</param> 
/// <param name="membersHeading">The name of the column that lists the public fields and properties of type T.</param>   
static DataTable Pivot<T>(this IEnumerable<T> collection, string headingMember, string membersHeading) 
{ 
    // get object information 
    var type = typeof(T); 
    var members = new List<MemberInfo>(); 
    members.AddRange(type.GetProperties(BindingFlags.Public | BindingFlags.Instance)); 
    members.AddRange(type.GetFields(BindingFlags.Public | BindingFlags.Instance)); 

    // create dataTable and establish schema 
    var dt = new DataTable(); 
    dt.Columns.Add(membersHeading); 
    foreach (var item in collection) 
    { 
     var member = members.Single(x => x.Name == headingMember); 
     if (member is FieldInfo)       
     { 
      FieldInfo fieldInfo = (FieldInfo)member; 
      dt.Columns.Add(fieldInfo.GetValue(item).ToString(), fieldInfo.FieldType); 
     } 

     if (member is PropertyInfo) 
     { 
      PropertyInfo propInfo = (PropertyInfo)member; 
      dt.Columns.Add(propInfo.GetValue(item, null).ToString(), propInfo.PropertyType); 
     } 
    } 

    // add rows to table 
    foreach (MemberInfo member in members.Where(x => x.Name != headingMember)) 
    { 
     var row = dt.NewRow(); 
     row[0] = member.Name; 
     int i = 1; 

     foreach (var item in collection) 
     { 
      if (member is FieldInfo) 
       row[i++] = ((FieldInfo)member).GetValue(item); 

      if (member is PropertyInfo) 
       row[i++] = ((PropertyInfo)member).GetValue(item, null); 
     } 

     dt.Rows.Add(row); 
    } 

    return dt; 
} 

Czy istnieje Co byłoby lepszym sposobem, aby to zrobić ?

+0

To jest duplikat wielu pytań (wyszukaj "pivot linq"). – jason

+0

Zrobiłem, zanim zapytałem. –

Odpowiedz

1

Możesz przestawić Liczbę I poprzez użycie grupy na nazwie. Zakładając, że nazwa jest odrębna. Po przestawieniu danych możesz Convert IEnumerable to DataTable.

+0

Czy możesz podać przykładowy kod? – Lucas

Powiązane problemy