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

本文关键字:一种 类型转换 类型 另一种 列表 数据 | 更新日期: 2023-09-27 18:12:08

我正在努力更好地学习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); 
}

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

给你:

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()
                });