views:

32

answers:

1

Is there a more elegant/concise way of this; I'd like to get rid of foreach loop with the WorkListItem initialization code.

        var queryable = registrations.Select(
            r => new
                     {
                         r.Id, r.AccountNumber, r.DateAdded, r.DateUpdated, r.Patient, r.Patient.InsuranceInfos
                     });
        var list = queryable.ToList();

        var workListItems = new List<WorkListItem>();
        foreach (var anonymous in list)
        {
            var w = new WorkListItem
                        {
                            Id = anonymous.Id,
                            ClientAccountId = anonymous.AccountNumber,
                            DateAdded = anonymous.DateAdded,
                            DateUpdated = anonymous.DateUpdated,
                            Patient = anonymous.Patient,
                            InsuraceInfos = anonymous.Patient.InsuranceInfos
                        };
            workListItems.Add(w);
        }
        return workListItems;
+2  A: 

Yes you can completely cut out the "middle-man" as it were and select straight into a new WorkListItem as below:

var list = registrations.Select(r => new WorkListItem
           {
               Id = r.Id,
               ClientAccountId = r.AccountNumber,
               DateAdded = r.DateAdded,
               DateUpdated = r.DateUpdated,
               Patient = r.Patient,
               InsuraceInfos = r.Patient.InsuranceInfos
           }).ToList();
DoctaJonez
or even add the ability for the `WorkListItem` class to be cast from a `registration `
PostMan
that works, thanks!
vikram nayak