?
Solved

Entity Framework Projection -

Posted on 2013-06-08
4
Medium Priority
?
468 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 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