【问题标题】:View Model cannot implicitly convert type System.Generic.Type.List<AssetViewModel> to System.Generic.Type.List<LUT_Asset_Masters>View Model 无法将类型 System.Generic.Type.List<AssetViewModel> 隐式转换为 System.Generic.Type.List<LUT_Asset_Masters>
【发布时间】:2017-11-19 10:40:29
【问题描述】:

我创建了一个视图模型并编写了一个 LINQ 查询来连接表,但我收到了这个错误。我花了两天时间弄清楚没有成功。看到类似的问题并进行了更改,但无法解决 LINquery 中的错误。 我的视图模型如图所示

  public class AssetViewModel

 {
        public string AG { get; set; }
    public string CC { get; set; }
    public string CS { get; set; }
    public string Mnf { get; set; }

 }

我的功能

    protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindCS();
        BindCC();
     BindAG();
        List<LUT_Assets_Masters> dt = this.GetData();
                  rptMarkers.DataSource = dt;
        rptMarkers.DataBind();
    }
}

   private List<LUT_Assets_Masters> GetData()
{
    AssetTaggingEntities context = new AssetTaggingEntities();
    List<LUT_Assets_Masters> am1 = null;

     if (DDSearch.SelectedValue == "1" && DDStatus.SelectedIndex > 0 && DDCondition.SelectedIndex > 0 && DDGroup.SelectedIndex > 0)

   {
        am1 = (from am in context.LUT_Assets_Masters
               where am.CSID == DDStatus.SelectedIndex
                     && am.CCID == DDCondition.SelectedIndex
                     && am.AGrpID == DDGroup.SelectedIndex
               select am).ToList();
    }
  else
    {
        am1 = (from am in context.LUT_Assets_Masters
              join grp in context.LUT_Asset_Groups on am.AGrpID equals grp.AGrpID
              join cc in context.LUT_Current_Condition on am.CCID equals cc.CCID
              join cs in context.LUT_Current_Status on am.CSID equals cs.CSID
              join mn in context.LUT_Asset_Manufacturers on am.MnfID equals mn.MnfID
              select new AssetViewModel
              {
                  AG = grp.Asset_Groups,
                  CC = cc.Current_Condition,
                  CS = cs.Current_Status,
                  Mnf = mn.Asset_Manufacturer
              }).ToList();
    }
           return am1;

}

【问题讨论】:

  • 我也尝试过返回 AssetViewModel 列表

标签: c# asp.net linq generics


【解决方案1】:

您的select 查询运算符正在选择AssetViewModel 类型的对象,然后ToList() 将返回的IEnumerable&lt;AssetViewModel&gt; 转换为List&lt;AssetViewModel&gt;,然后您尝试将其分配给am1,但该列表包含对象LUT_Assets_Masters 类型的对象因此出现错误,因此您可能必须更改 select 查询运算符以选择 LUT_Assets_Masters 类型的对象或更改列表以保存 AssetViewModel 类型的对象。

【讨论】:

  • 什么?我需要 List Asset Masters 和 Asset View Model 的成员。你能在代码中解释一下吗?为什么我可以问@Aomine
  • @ARUS 好吧,您不能将List&lt;AssetViewModel&gt; 类型的列表分配给List&lt;LUT_Assets_Masters&gt; 类型。再次阅读我的答案,因为这是您仅有的两个选择。我没有对你投反对票……
  • 我阅读了您的答案,但我也想要来自 Asset Masters 的数据。你认为什么是合适的,因为我需要两者的数据。生病更新我的代码。谢谢你的协助。你能告诉我正确的方法吗?
  • 非常感谢您帮助我解决这个问题,但实际上如果条件也需要此连接查询。我实际上正在尝试的是获取 Groups 和 Condition 表中的下拉列表的值,而其他详细信息位于 Asset Masters 表中。所以我需要javascript中的两个值。所以我尝试了视图模型。如何在 if 语句中包含 join?
  • 我发现问题和你说的一样 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 2020-01-14
  • 1970-01-01
相关资源
最近更新 更多