Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Entity Framework Projection -

Posted on 2013-06-08
4
Medium Priority
?
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 23

Assisted Solution

by:Ioannis Paraskevopoulos
Ioannis Paraskevopoulos earned 1500 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 1500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

650 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