Solved

Entity Framework Projection -

Posted on 2013-06-08
4
433 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now