【问题标题】:How can i fix Cannot implicitly convert Generic.List< > to Generic.List< >我该如何修复无法将 Generic.List<> 隐式转换为 Generic.List<>
【发布时间】:2020-04-17 20:05:30
【问题描述】:

如何修复无法将 System.Collections.Generic.List &lt;RPHistory&gt; 隐式转换为 System.Collections.Generic.List &lt;RPHistory&gt; 异常错误。

我正在尝试将两个实体组合在一起以获得一个列表

RP 实体类:

 public class RP 
        {
            public int ID { get; set; }
            public int RPID { get; set; }
            public string Name { get; set; }
            public int ProductID { get; set; }
        }

RPHistory 实体类:

public class RPHistory: 
    {
        public int ID { get; set; }
        public int RPID { get; set; }
        public string Name { get; set; }
        public int ProductID { get; set; }
    }

我创建了第三个类

RpWithHistory 类:

public class RpWithHistory {
  public int ID;
  public int RPID;
  public string Name;
  public int ProductID;

  public List<RPHistory> History;
}

Linq 查询

var RPs = await Context.RP.Where(b => b.ProductID  == request.ID)
                              .Select(x=> new RpWithHistory {
                                               ID = x.ID, 
                                               RPID = x.RPID, 
                                               Name = x.Name, 
                                               ProductID = x.ProductID, 
                                               History = Context.RPHistory
                                                                .Where(y=> y.RPID 
                                                                        == x.RPID)
                                                                .ToList()
                                               }
                                      ).ToListAsync();

但我得到了这个错误,

>Cannot implicitly convert System.Collections.Generic.List <RPHistory> to
>System.Collections.Generic.List <RPHistory> exception error

谢谢!

【问题讨论】:

  • public List&lt;RPHistory&gt; History; 是属性还是变量?
  • @mvermef 感谢它的属性
  • 你确定这两个 RPHistory 类型真的是相同的类型吗?如果您右键单击类型并选择“转到定义”,它们是否指向同一个地方?如果是这样,我不明白为什么会显示任何错误
  • RP 和 RPHistory 相同的事实本身就很奇怪......

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


【解决方案1】:

我不确定你为什么要这样做。我可以建议吗?

您无需如此创建一个连接两者的类。只需在您的 RP 上创建一个指向 RPHistory 对象的 Navigation 属性。

public class RP 
{
    public int ID { get; set; }
    public int RPID { get; set; }
    public string Name { get; set; }
    public int ProductID { get; set; }
    public ICollection<RPHistory> HistoryList { get; set; } // Navigation Property
}

public class RPHistory: 
{
    public int ID { get; set; }
    public int RPID { get; set; }
    public string Name { get; set; }
    public int ProductID { get; set; }

    [ForeignKey(nameof(RPID))] // Identify the Foreign Key from RP Class
    public RP RP { get; set; } // Navigation back to RP
}

然后您可以使用 LINQ 将所有内容链接到一个列表中:

var RPs = Context.RP.Where(rp => rp.ProductID  == request.ID)
                 .Include(rp=>rp.RPHistory) // This includes RPHistory
                 .ToList();

【讨论】:

  • 但是一件事看它内部连接的日志 INNER JOIN [dbo].[RPHistory] ​​AS [r] ON [v].[ProductID] = [r].[RPID] - 我想ProductID=ProdcutID 时加入
  • RP ProductID 等于 RPHistory ProductID 时要加入吗?我不明白那里。
  • 是的,Mosia,这就是我想要的,但 ProductID 不是两个表中的主键。谢谢!顺便说一句,你让我开心:)
  • RPID 有什么不同吗?因为如果 RPID 相同,那么您不必应用 ProductID,EF 将默认使用 RPID foreignKey 根据 RPID 获取 RPHistory 对象......我总是建议使用任何定义属性作为两个表之间的共性,因为那时它变得更容易。根据表格的设计,我认为 RPID 不会相同,而 ProductID 会不同,还是我错了?
  • RPID 不同。 RP 和 RP 历史表中的 productID 必须相同才能显示它们,但 RPID 不同,RPHistory 表存储已删除的 rps
【解决方案2】:

您需要克隆或创建一个新列表。

选项 1:使用 ConvertAll

List<RPHistory> pPHistoryCopy = rphWithHistory.RPHistory.ConvertAll(history => new RPHistory(rphWithHistory.RPHistory));

选项 2:

//Clone Extension
static class Extensions
{
    public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }
}

使用克隆扩展

【讨论】:

  • 谢谢,在 linq 之后我可以在哪里使用 ConvertAll?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多