Leron
Leron

Reputation: 9876

Create one List from several different lists of the same type with C#

I am working on a asp.net mvc 3 application. I am using LINQ to fetch data from the database :

 public object GetFieldsForDocument()
        {
            DocumentFieldsRepository.Context = FieldsRepository.Context;

            var model = DocumentFieldsRepository.All()
                    .Join(FieldsRepository.All(),
                        df => df.FieldId,
                        f => f.Id,
                        (df, f) => df).ToList();
            return model;
        }

and then in my controller I have this :

List<MCS_DocumentFields> model = (List<MCS_DocumentFields>)DocumentFieldsService.GetFieldsForDocument();
            model = model.Where(x => x.DocumentId == 10005)
                .OrderBy(x => x.ContentTypeId)
                .ThenBy(x => x.RowNo)
                .ThenBy(x => x.ColumnNo)
                .ToList();

I know that this could be avoid by passing the ID to the GetFieldsForDocument() method but it's very early stage and for now I just want to make things work.

So... Now that I have all the data from the MCS_DocumentFields entity for a given Id I want to separate the data as it is logically separated when it's rendered from the view. I have property ContentType which can be Header, Drawing, Body, Footer. So I want to be able to separate those parts because I have partial views for each content type. So I make this :

var headerItems = model.Where(c => c.ContentTypeId == 1).GroupBy(c => c.RowNo).ToList();
            var drawItem = model.Where(d => d.ContentTypeId == 2).ToList();
            var bodyItems = model.Where(b => b.ContentTypeId == 3).GroupBy(b => b.RowNo).ToList();
            var footerItems = model.Where(f => f.ContentTypeId == 4).ToList();

but now I need to combine this to one data structure of type MCS_DocumentFields so I can pass it to my view and then from the view with foreach statement to display the data using my partial views.

I'm not sure how exactly to combine those four parts in one keeping the separation between the different parts.

Now the Visual Studio is showing that the four var variables are of type IGrouping<Int32, MCS_DocumentFields>

Upvotes: 1

Views: 119

Answers (2)

Nick Frederiksen
Nick Frederiksen

Reputation: 71

I would use Union instead, but you can use the Concat approach above by adding "SelectMany(i=>i)":

var MCS_DocumentFields = headerItems.SelectMany(i => i)
                            .Concat(drawItem)
                            .Concat(bodyItems.SelectMany(i => i))
                            .Concat(footerItems)
                            .ToList();

And I would highly recommend not using ToList(). Use AsEnumerable() instead. Each .ToList() loops the Collection, and with AsEnumerable() you only loop once.

Upvotes: 2

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

You can use concat function like this

var MCS_DocumentFields = headerItems.Concat(drawItem)
                                .Concat(bodyItems)
                                .Concat(footerItems)
                                .ToList();

Upvotes: 2

Related Questions