【问题标题】:Linq GroupBy anonymous type error MVC4Linq GroupBy 匿名类型错误 MVC4
【发布时间】:2014-04-20 21:26:13
【问题描述】:

我有下面的代码,它使用组来选择不同的值。智能和代码中断显示查询按预期工作。

public ActionResult _PortfoliosbyCountry()
    {
        var portfolios = db.Portfolios;
        var query = (from t in portfolios
                     group t by new { t.UserId, t.CountryId,t.PortfolioTypeId }
                         into grp
                         select new 
                         {
                             grp.Key.CountryId,
                             grp.Key.PortfolioTypeId,
                             grp.Key.UserId  
                         }).ToList();


        return PartialView(query);
    }

型号

namespace RefST.Models
{
public class Portfolio
{
    [Key]
    public int PId { get; set; }
    [Required]
    public string Title { get; set; }
    [ScaffoldColumn(false)]
    public DateTime DatePosted { get; set; }
    [ScaffoldColumn(false)]
    public DateTime LastEdited { get; set; }
    [AllowHtml][Required]
    public string Body { get; set; }
    public int UserId {get; set; }     
    public int CountryId { get; set; }
    public int PortfolioTypeId { get; set; }
    public virtual Country Country { get; set; }
    public virtual PortfolioType PortfolioType { get; set; }
}
}

问题是剃刀视图,它给出了以下错误 传入字典的模型项的类型是:'System.Collections.Generic.List1[<>f__AnonymousType193[System.Int32,System.Int32,System.Int32]]',但是这个字典需要一个模型项输入“System.Collections.Generic.IEnumerable`1[RefST.Models.Portfolio]”

@model IEnumerable<RefST.Models.Portfolio>

<p>
   @Html.ActionLink("Create New", "Create")
</p>

<table>
 <tr>

    <th>
        @Html.DisplayNameFor(model => model.UserId)
    </th>

    <th>
        @Html.DisplayNameFor(model => model.CountryId)
    </th>


    <th>
        @Html.DisplayNameFor(model => model.PortfolioTypeId)
    </th>

    <th></th>
 </tr>

 @foreach (var b in Model) {
  <tr>
    <td>
        @Html.DisplayFor(modelItem => b.UserId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => b.CountryId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => b.PortfolioTypeId)
    </td>
  </tr>
 }
 </table>

如果有人能指出正确的方向,我将不胜感激

【问题讨论】:

  • 你能显示你的PortFolio模型的代码吗?
  • 报错是说你是匿名分组,select new 后添加 Portfolio。所以选择新的投资组合{grp.key。等}。

标签: c# asp.net-mvc linq razor


【解决方案1】:

我认为在这一行中你可以这样做:

select new PortFolio
                     {
                        countryId= grp.Key.CountryId,
                        PortfolioTypeId= grp.Key.PortfolioTypeId,
                        UserId= grp.Key.UserId,
                        //Add others members with default value
                        Titel=string.Empty;
                        ....  
                     }).ToList();

所以您确定您将IEnumerable&lt;Portfolio&gt; 作为Model 发送

【讨论】:

  • 我尝试过您的推荐,但得到进一步的错误实体或复杂类型 'RefST.Models.Portfolio' 无法在 LINQ to Entities 查询中构造。
  • 您是否建议我为必填字段创建视图模型?
  • 你也可以。当您在 Portfolio 中仅使用 3 个属性时。但与此同时,您可以在其他地方更改代码。明智地考虑一下。
  • 我为这 3 个属性创建了一个 PortfolioFilterViewModel,如果我只是将 select new Portfolio 替换为 select new PortfolioFilterViewModel,它就可以正常工作
【解决方案2】:

如果你去掉过多的 .NET 通用语法,错误消息就很清楚出了什么问题:

The model item passed into the dictionary is of type: 'List&lt;AnonymousType&lt;int, int, int&gt;&gt;', but this dictionary requires a model item of type 'IEnumerable&lt;Portfolio&gt;'.

在您的代码中,如果您忽略分组,您实际上是在这样做:

from x in y ... 
select new { CountryId = x.a, PortfolioTypeId = x.b, UserId = x.c }

生成一个恰好具有三个整数字段的匿名类型。尽管您没有为该类型命名,但 编译器 已为其命名,这是您在 C# 中永远无法为您自己的类型使用的名称(在本例中,它称为 &lt;&gt;f__AnonymousType19 )。

当两个匿名类型以相同的顺序具有相同类型的相同字段时,编译器会将它们视为相同类型。但是,匿名类型从不与您自己命名的任何类型相同,编译器不会自动在它们之间进行转换。您的 Razor 视图需要一个类型为 Portfolio 的对象列表,而您向它发送了一个不是该类型的对象列表。您的匿名类型恰好具有正确的三个字段这一事实与编译器无关——这只是 错误的类型

幸运的是,这很容易解决。在 LINQ 查询中选择正确的命名类型:

from x in y ... 
select new Portfolio { CountryId = x.a, PortfolioTypeId = x.b, UserId = x.c }

【讨论】:

  • 这确实提供了很多信息,但我会打勾 inanikan 因为他是第一个提供帮助的人
  • @Diin - 你可以给他们两个投票以表明他们是好的和有用的答案(就像我一样)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多