Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Every time I run this I get an error:

Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType1 2[System.String,System.String]]' to type 'System.Collections.Generic.IEnumerable1[CRUD__MVC.Models.Customer]

What I have tried:

//Scenario 1
C#
public ActionResult FirstLastName()
{
    return View(Name()); 
}

IEnumerable<customer>Name()   
{
    using (AdventureWorksLTDataContext db = new AdventureWorksLTDataContext())
    {
        return (IEnumerable<customer>)db.Customers.Select(c => new { FirstName = c.FirstName, LastName = c.LastName }).ToList();
    }
}



//Scenario 2
C#
IEnumerable<customer> Name()   
{
            
return db.Customers.Select(c => new Customer { FirstName = c.FirstName, LastName = c.LastName });
            
}
Posted
Updated 20-Jan-22 7:56am
v2

<pre>Use the following code:

IEnumerable<customer>Name()   
{
    using (AdventureWorksLTDataContext db = new AdventureWorksLTDataContext())
    {
        return (IEnumerable<customer>)db.Customers.Select
		(c => new customer //specify customer type!!
						{ 
							FirstName = c.FirstName, 
							LastName = c.LastName 
						}
		).ToList();
    }
}

You are returning anonymous type and missing the specific type.
So this List<customer> is a totally different type than a 
IEnumerable<customer>.
 
Share this answer
 
C#
Well we can generate a class that derives from my LINQ to SQL class

internal class CustomerView: Customer { }

And rewrite the query as.

IEnumerable<customer> Name()   
{
    using (AdventureWorksLTDataContext db = new AdventureWorksLTDataContext())
    {
        var query= db.Customers.Select(c => new CustomerView { FirstName = c.FirstName, LastName = c.LastName }).ToList();
        //Cast it back to Customer
        return (query.Cast<customer>())
    }
}
 
Share this answer
 
v2
Can Anyone tell me how to create a view for that.

This is what I tried.




@model IEnumerable<CRUD__MVC.Models.Customer>

<table class="table">
    <tr>
        
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
       
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        
    </tr>
}

</table>
 
Share this answer
 
Comments
Richard Deeming 21-Jan-22 4:17am    
Your follow-up question is not a "solution" to the question. By marking it as the accepted solution, you appear to be trying to "cheat" the system in order to increase your reputation. That's a good way to get yourself banned from the site.

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