user3429672
user3429672

Reputation: 237

Linq - adding multiple different objects to a list

I am trying to add a range of values from four distinct list of objects to a list. This is the code I have to add all the items from one list of objects...

var formList = new List<FormList>();

    formList = forms.LimitedWillForms.Select(a => new FormList()
                {
                    DateCreated = a.CreationDate,
                    FormId = a.Id,
                    FormType = a.FormType,
                    Submitted = a.SubmissionDate != null
                }).ToList();

I am trying to not just add from the forms.LimitedWillForms list, but also from the forms.FullWillForms, and the forms.FullWillForms2, and the forms.FullWillForms3 too, adding the same parameters. This seems to work to add selected parameters from the form to the list.

I am not sure of the most efficient way to use linq to add selected parameters from all four lists to the formList. Can anyone help?

Upvotes: 0

Views: 1866

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

Try this:

var formList = forms.LimitedWillForms.Select(a => new FormList
                    {
                        DateCreated = a.CreationDate,
                        FormId = a.Id,
                        FormType = a.FormType,
                        Submitted = a.SubmissionDate != null
                    })
                    .Union(forms.FullWillForms.Select(a => new FormList
                    {
                        DateCreated = a.CreationDate,
                        FormId = a.Id,
                        FormType = a.FormType,
                        Submitted = a.SubmissionDate != null
                    }))
                    .Union(forms.FullWillForms2.Select(a => new FormList
                    {
                        DateCreated = a.CreationDate,
                        FormId = a.Id,
                        FormType = a.FormType,
                        Submitted = a.SubmissionDate != null
                    }))
                    .Union(forms.FullWillForms3.Select(a => new FormList
                    {
                        DateCreated = a.CreationDate,
                        FormId = a.Id,
                        FormType = a.FormType,
                        Submitted = a.SubmissionDate != null
                    })).ToList();

Upvotes: 0

Magnus
Magnus

Reputation: 47026

Since the lists contain objects of different types your best option would be to add an common interface to all the types for the common properties.

public interface IRecord
{
   DateTime DateCreated {get;set;}
   int FormId {get;set;}
   ....
}

You can then do:

var formList = forms.LimitedWillForms
               .Cast<IRecord>
               .Concat(forms.FullWillForms)
               .Concat(forms.FullWillForms2)
               .Concat(forms.FullWillForms3)
               .Select(x => new FormList()
               {
                   DateCreated = x.CreationDate,
                   FormId = x.Id,
                   FormType = x.FormType,
                   Submitted = x.SubmissionDate != null
               }).ToList();

If you can live with just getting back a list if IRecord instead of FormList you can actually skip the last select.

If that is not possible you would need to select the properties from each collection.

var formList = forms.LimitedWillForms.Select(x => new FormList()
                {
                    DateCreated = x.CreationDate,
                    FormId = x.Id,
                    FormType = x.FormType,
                    Submitted = x.SubmissionDate != null
                }).Concat(
                    forms.FullWillForms.Select(x => new FormList()
                    {
                       DateCreated = x.CreationDate,
                       FormId = x.Id,
                       FormType = x.FormType,
                       Submitted = x.SubmissionDate != null
                    }
                ).Concat(...).ToList();

Upvotes: 1

Related Questions