0

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?

4
  • So you just want to union several lists together? Commented Jun 10, 2016 at 10:39
  • just 4 parameters from each item in each list, all put into a single list - ie there might be twenty properties in each object in the list (and each list will have different objects in, but with some parameters the same), but I just want the created date, id, type, and whether it has been submitted or not, which will be available in each object in each list. Commented Jun 10, 2016 at 10:41
  • Does the lists contain objects of the same type? Commented Jun 10, 2016 at 10:47
  • no - they are of different type, but all four lists contain objects which contain the same parameters (creationDate, Id, FormType, and SubmissionDate) but also contain other unique parameters not found in the other objects in the other lists. Commented Jun 10, 2016 at 10:48

2 Answers 2

1

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

2 Comments

Hi - I have got this to work using your second method. I don't think I can use the first, as the forms are from entity framework. Do you know off the top of your head whether entity framework models can work with interfaces?
I think the entity classes generated by entity framework are partial. That means if you add your own partial class with the same name that can implement the interface you want.
0

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

1 Comment

Thanks for your time on this. This solution is producing an error one each list ( non-invocable member 'Form.LimitedWillForms' cannot be used like a method.) - I am not sure why, but Magnus solution using .concat seems to work well. Thanks.

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.