Solved

Entity Framework Projection -

Posted on 2013-06-08
4
445 Views
Last Modified: 2013-06-13
Hi:

I have an application that I am trying to convert from web forms to MVC using EntityFramwork 5.0.

In my original application I call a stored procedure that returns a list of Lessons (Modules) that includes Name Of Module and Action.  For the action column, if it returns a 1 (meaning IsComplete), I replace it with an anchor tag and text that says `Review Lesson`, otherwise I say "Take Lesson".

In my new application, I would like to implement this in my ModuleRecords repository.  Below is my code and pseudo code for doing this.  Any help in doing this would be greatly appreciated...

 public IEnumerable<ModuleRecord> GetModuleRecordsByEnrollmentId(int enrollmentId)
        {
            return DbSet.Where(e => e.EnrollmentId == enrollmentId)
                        .Select(m => new ???
                            {
                                Name= m.Module.Name,
                                Action = If IsCompleted, say "Review Lesson" otherwise say "Take Lesson"

                            } );
        }

Open in new window

0
Comment
Question by:RBS
  • 2
  • 2
4 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 500 total points
ID: 39232899
Hi, if you have a corresponding entity you can add it after new. For instance if there is a ModuleEntity ( you may create one in your model) then you can say
.Select(m=> new ModuleEntity

Open in new window


Now for the if statement you can have:
Action = IsCompeted == true ? "Review Lesson" : "Take Lesson"

Open in new window


Sorry for not adding all this up, but i am writting from my mobile and copy paste is a littlt bot difficult.

Giannis
0
 

Author Comment

by:RBS
ID: 39233608
Thank you for your suggestion Giannis.  I have taken it and created the following code:

public IEnumerable<ModuleRecord> GetModuleRecordsByEnrollment(Enrollment enrollment)
        {
            return DbSet.Where(e => e.EnrollmentId == enrollment.Id)
                        .OrderBy(e => e.CatalogueNumber )
                        .Select(m => new ModuleRecord()
                        {
                            Name = m.Module.Name,
                            Action = m.IsEnabled == 1 ? "Take Lesson " : "",
                            CatalogueNumber = m.CatalogueNumber,

                        });

        }

Open in new window


When this gets to the html part which is as follows:

@model IEnumerable<CodeCamper.Model.ModuleRecord>
 @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CatalogueNumber)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>

                <td>
                     @Html.DisplayFor(modelItem => item.Action)
                </td>
            </tr>
        }

Open in new window


I get the following error:  
The entity or complex type 'CodeCamper.Data.ModuleRecord' cannot be constructed in a LINQ to Entities query.

Any suggestions?

Thanks,
RBS
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39233949
Hi,

It seems that you cannot create new Entities that way. You may have a look at http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query

On that post there is a suggestion (the third one) of doing it like this:
public IEnumerable<ModuleRecord> GetModuleRecordsByEnrollment(Enrollment enrollment)
        {
            var ModuleRecords = DbSet.Where(e => e.EnrollmentId == enrollment.Id)
                        .OrderBy(e => e.CatalogueNumber ).ToList()
                        .Select(m => new ModuleRecord()
                        {
                            Name = m.Module.Name,
                            Action = m.IsEnabled == 1 ? "Take Lesson " : "",
                            CatalogueNumber = m.CatalogueNumber//,
                        });
            return ModuleRecords;
        }

Open in new window


So it may help. Otherwise you would have to create a non entity class and populate that.

Giannis
0
 

Author Closing Comment

by:RBS
ID: 39245588
Sorry for the delay responding - yes, can't pass in temp entities created in the procedure and then pass them on.  pointed to existing class and passed that on and it worked.

Thanks for the suggestions.

RBS
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question