causita
causita

Reputation: 1708

MVC Unable to display Linq query result to view

I'm trying to display linq results into a view but I have not been able to. The error I get is "The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType22[System.Double,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable"

I created my view manually and by right clicking the controller and adding a view but I'm kind of stuck here.

public ActionResult leftjoin()
     {
        var q = from b in db.OrderHistories
                 join c in db.BuyerComms on b.ITEMID equals c.ITEMID into sr
                 from x in sr.DefaultIfEmpty()
                 select new { Item = b.ITEMID, buyer = x.Buyer };
                 return View (q.ToList());


     }

and my view:

@model IEnumerable<myapp.Models.OrderHistory>

I used linqpad to test my linq and I'm getting the right results.

Upvotes: 0

Views: 1164

Answers (1)

Justin Skiles
Justin Skiles

Reputation: 9523

Your query is returning an anonymous type as indicated by:

select new { Item = b.ITEMID, buyer = x.Buyer };

You need to instead select into a type of OrderHistory. You didn't provide that class, so I'm going to guess that it's something like:

select new OrderHistory { Item = b.ITEMID, buyer = x.Buyer };

Upvotes: 3

Related Questions