• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Entity Framework Projection -

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
RBS
Asked:
RBS
  • 2
  • 2
2 Solutions
 
Ioannis ParaskevopoulosCommented:
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
 
RBSAuthor Commented:
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
 
Ioannis ParaskevopoulosCommented:
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
 
RBSAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now