将C#列表中的数据从一种类型转换为另一种类型。

时间:2014-07-09 01:44:06

标签: c# linq

我正在努力学习Linq,所以如果这是一个愚蠢的问题我提前道歉。

考虑以下类别:

class FormData 
{
   public string CustomerId {get; set;}
   public string FieldId {get; set;} 
   public string FieldValue {get; set;}
}

class CustomerDetails 
{
   public string CustomerId {get; set;}
   public FormField[] Fields {get; set;} 
}

class FormField
{
   public string FieldId {get; set;}
   public string FieldValue {get; set;} 
}

我正在尝试转换此数据

// example incoming data set 
List<FormData> formData = new List<FormData> {
   new FormData { CustomerId = 1, FieldId = "FirstName", FieldValue = "Peter" },   
   new FormData { CustomerId = 1, FieldId = "LastName", FieldValue = "Smith" },   
   new FormData { CustomerId = 1, FieldId = "PetsName", FieldValue = "Spot" },  
   new FormData { CustomerId = 2, FieldId = "FirstName", FieldValue = "Dougie" },   
   new FormData { CustomerId = 2, FieldId = "LastName", FieldValue = "Fresh" },   
   new FormData { CustomerId = 2, FieldId = "PetsName", FieldValue = "Skittles" }, 
   new FormData { CustomerId = 3, FieldId = "FirstName", FieldValue = "Sam" },   
   new FormData { CustomerId = 3, FieldId = "LastName", FieldValue = "Reynolds" },   
   new FormData { CustomerId = 3, FieldId = "PetsName", FieldValue = "Taco" }              
};

到此

// desired out going data set 
List<CustomerDetails> customerDetails = new List<CustomerDetails> {
   new CustomerDetails {
    CustomerId : 1, 
    Fields : [
           new FormField {FieldId = "FirstName", FieldValue = "Peter"}, 
           new FormField {FieldId = "LastName", FieldValue = "Smith"},
           new FormField {FieldId = "PetsName", FieldValue = "Spot"}
        ]
    },
   new CustomerDetails {
    CustomerId : 2, 
    Fields : [
           new FormField {FieldId = "FirstName", FieldValue = "Dougie"}, 
           new FormField {FieldId = "LastName", FieldValue = "Fresh"},
           new FormField {FieldId = "PetsName", FieldValue = "Skittles"}
        ]
    },
   new CustomerDetails {
    CustomerId : 3, 
    Fields : [
           new FormField {FieldId = "FirstName", FieldValue = "Sam"}, 
           new FormField {FieldId = "LastName", FieldValue = "Reynolds"},
           new FormField {FieldId = "PetsName", FieldValue = "Taco"}
        ]
    }
}; 

以下是我目前正在做的事情......还有更好的方法吗?

var customerIds = formData.Distinct().Select(x => x.CustomerId).ToList();

var results = new List<CustomerDetails>(); 

foreach (var id in customerIds)
{
    var customerDetails = new CustomerDetails();
    customerDetails.CustomerId = id;

    var fields = new List<GuestItemDataFieldResult>(); 

    foreach (var f in formData.Where(x => x.CustomerId == id))
    {
    var field = new FormField();
        field.FirstName = f.FirstName; 
        field.LastName = f.LastName; 
        field.PetsName = f.PetsName;

        fields.Add(field); 
    }

    customerDetails.Fields = fields.ToArray(); 

    results.Add(customerDetails); 
}

1 个答案:

答案 0 :(得分:7)

你走了:

var newData =
    formData.GroupBy(g1 => g1.CustomerId)
        .Select(
            s1 =>
                new CustomerDetails
                {
                    CustomerId = s1.Key.ToString(),
                    Fields =
                        s1.Select(s2 => new FormField {
                                FieldId = s2.FieldId, 
                                FieldValue = s2.FieldValue})
                            .ToArray()
                });