Solved

MVC two joing table with link querry order by

Posted on 2014-10-22
9
152 Views
Last Modified: 2016-02-18
I have this controller that return a grid but I would like to make some modification so my search result are order by location.

I have a table Master List that contain LastName, FirstName, and others fields and has one foreign key that is LocationID that is related to a Table phoneList Location and its primary key is ID.

My current controller work and return a result set:
var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);

Open in new window

But when I made some modification bellow  I get an error:

     var masterLists = from r in db.MasterLists.Include(m => m.PhoneListLocation)
                              orderby r.PhoneListLocation.ID
                              select r.PhoneListLocation;
                              




            return View(masterLists.ToList());       

Open in new window


The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[PhoneListTest.Models.PhoneListLocation]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[PhoneListTest.Models.MasterList]'.
0
Comment
Question by:yguyon28
  • 4
  • 3
  • 2
9 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40397066
Hi yguyon28;

The two queries are returning different data types. The first is returning a collection of MasterLists while the modified query that does not work returns collection of PhoneListLocation. While I do not work with Web applications I believe that the View is not expecting the PhoneListLocation data type.

What will be needed in the View?
0
 

Author Comment

by:yguyon28
ID: 40397077
Well this controller work

var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);

but I'm trying to modified it so I can have an orderby from the location in the phoneListLocation...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40397096
What is the relationship between MasterLists and PhoneListLocation? Is it a One to One, One to Many?
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40397107
I think your hangup may be here:
var masterLists = (from r in db.MasterLists.Include(m => m.PhoneListLocation)
		   orderby r.PhoneListLocation.ID
		   select r.PhoneListLocation);
// Not this - return View(masterLists.ToList());
// Use this instead:
return View(masterLists);

Open in new window

masterLists should already be a 'System.Collections.Generic.IEnumerable`1[PhoneListTest.Models.MasterList]'.

-saige-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:yguyon28
ID: 40397392
But I get this error when I run the code....

Server Error in '/' Application.

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[WebApplicationCodeFirstPhoneList.Models.PhoneListLocation]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplicationCodeFirstPhoneList.Models.MasterList]'.
0
 

Author Comment

by:yguyon28
ID: 40397409
the relation is one to many
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40397457
Is your View method doing any sort of conversion?  What does the view method take as a parameter?

-saige-
0
 

Author Comment

by:yguyon28
ID: 40397477
No no conversion

@model IEnumerable<WebApplicationCodeFirstPhoneList.Models.MasterList>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PhoneListLocation.Location)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Department)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExtensionInternal)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ExtensionDirectDial)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberMobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberHome)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberTextMessage)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NumberOther)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Terminated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Shift)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Notes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NameGrouping)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastUpdatedDT)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PhoneListLocation.Location)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Department)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionInternal)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ExtensionDirectDial)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberMobile)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberHome)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberTextMessage)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberOther)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Terminated)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Shift)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Notes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameGrouping)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastUpdatedDT)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.RecordID }) |
            @Html.ActionLink("Details", "Details", new { id=item.RecordID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RecordID })
        </td>
    </tr>
}

</table>

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40398119
Ordering of the PhoneListLocation collection which is part of the Include of the MasterLists entity can not be done in the Linq query, it will only return it in order that it has it in.  If you will not be updating the entities in the data context you can order them when they are materialize on the local system. Something like this should work.

var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);

foreach( var m in masterLists )
{
    List<Order> pl = m.PhoneListLocation.OrderBy(p => p.ID).ToList();
     m.PhoneListLocation = null;
     m.Orders = pl;
}

return View(masterLists)

Open in new window

;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

22 Experts available now in Live!

Get 1:1 Help Now