简体   繁体   English

MVC无法显示Linq查询结果以查看

[英]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. 我正在尝试将linq结果显示在视图中,但我无法这样做。 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" 我得到的错误是“传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery 1[<>f__AnonymousType2 2 [System.Double,System.String]]',但是此字典需要一个模型“ 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. 我使用linqpad测试了linq,并得到了正确的结果。

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 . 您需要改为选择一种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 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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