简体   繁体   中英

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.DbQuery 1[<>f__AnonymousType2 2[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.

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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM