Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting below error while calling LINQ queries to join

The model item passed into the dictionary is of type 'System.Linq.Enumerable+<JoinIterator>d__38`4[OnlineApplicationTest.Models.SubCategoryMaster,OnlineApplicationTest.Models.CategoryMaster,System.Int32,<>f__AnonymousType1`3[System.String,System.String,System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[OnlineApplicationTest.Models.SubCategoryMaster]'.


There is no column CategoryName in SubCategoryMaster table however it has only CategoryId is present.
I need to display CategoryName to display on view instead of Category.So i have written LINQ query to get the categoryName and passed result to view. However i am getting above error message. Any help on this will be appreciated. Thanks in advance.

What I have tried:

CategoryMaster table
CategoryId
CategoryName etc

SubCategoryMaster table
SubCategoryId
CategoryId
SubCategoryName


OnlineTestEntities2 objOnlineTest = new OnlineTestEntities2();
            
            List<SubCategoryMaster> Subcategories = objOnlineTest.SubCategoryMasters.ToList();
            List<CategoryMaster> categories = objOnlineTest.CategoryMasters.ToList();

            //var innerJoin = new List<SubCategoryMaster>();
            var innerJoin = (from s in Subcategories // outer sequence
                            join st in categories //inner sequence 
                            on s.CategoryId equals st.CategoryId // key selector 
                            select new
                            { // result selector 
                                SubCategoryName = s.SubCategoryName,
                                CategoryName = st.CategoryName,
                                SubCategoryId=s.SubCategoryId
                            }).AsEnumerable(); 
                       
            // d.AddRange(innerJoin);
            return View(innerJoin);



SubCategoryView as below

@model IEnumerable<OnlineApplicationTest.Models.SubCategoryMaster>

@{
    ViewBag.Title = "GetAllSubCategory";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>GetAllSubCategory</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CategoryId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SubCategoryName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.CategoryId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SubCategoryName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.SubCategoryId }) |
            @Html.ActionLink("Details", "Details", new { id=item.SubCategoryId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.SubCategoryId })
        </td>
    </tr>
}

</table>
Posted
Updated 15-Apr-20 4:12am

1 solution

You are selecting

new { .... }


as the return type in your linq query. This creates a collection of anonymous types, but your view needs a collection of SubCategoryMaster objects. You need to change your linq from "select new" to

var innerJoin = (from s in Subcategories // outer sequence
                            join st in categories //inner sequence 
                            on s.CategoryId equals st.CategoryId // key selector 
                            select new SubCategoryMaster
                            { // map the properties from your query to
                              // properties of SubCategoryMaster
                            }).AsEnumerable();
 
Share this answer
 
Comments
Member 7992716 16-Apr-20 6:56am    
Thanks for reply. I have tried this option but no luck its not working same error is coming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900