1

I am trying to learn Linq a little better so I apologies in advance if this is a silly question.

Given the following classes:

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

I'm trying to convert this data

// 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" }              
};

to this

// 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"}
        ]
    }
}; 

Here's how I'm currently doing it... there's gotta be a better way right?

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); 
}
7
  • 1
    What would you consider to be better? Less code? Faster processing? Commented Jul 9, 2014 at 2:00
  • @mclaassen I believe GroupBy is what I'm looking for but I can't wrap my head around the syntax (pretty new to LINQ still). Commented Jul 9, 2014 at 2:04
  • @paqogomez can't install any third party libraries per client specs... weak! Commented Jul 9, 2014 at 2:12
  • Talk to someone, modify the specs. JSON.Net is the way to handle json objects, which is what you're creating. Commented Jul 9, 2014 at 2:14
  • 1
    Not being able to use 3rd party libs is a silly requirement. Commented Jul 9, 2014 at 2:16

1 Answer 1

6

Here you go:

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()
                });
Sign up to request clarification or add additional context in comments.

1 Comment

that s1 => on it's own line is killing me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.