【问题标题】:Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection<Fast.Models.Reviews>'无法将类型“字符串”隐式转换为“System.Collections.Generic.ICollection<Fast.Models.Reviews>”
【发布时间】:2016-08-24 09:58:18
【问题描述】:
string updatedVotes = string.Empty;
updatedVotes = updatedVotes.Substring(0, updatedVotes.Length - 1);

db.Entry(sch).State = EntityState.Modified;
sch.Rates = updatedVotes;
db.SaveChanges();

但是由于您在上面看到的错误,代码无法编译,我还没有完全理解为什么。有什么帮助吗?

【问题讨论】:

  • updatedVotes 的类型为 string。 sch.Rates 的类型为 ICollection&lt;Fast.Models.Reviews&gt;。您不能像那样将不同类型的变量分配给彼此。那是 C# 101。

标签: c# asp.net-mvc


【解决方案1】:

updatedVotes 是一个字符串,但您正试图将其分配给 Rates(我假设它是 Fast.Models.Reviews 的集合)。

你有错误的类型。猫不能转换为狗,因此字符串不能转换为 System.Collections.Generic.ICollection

【讨论】:

  • 如果进化发生如下:猫:狗:动物。谁知道;)
【解决方案2】:

错误消息几乎可以告诉您发生了什么。在这一行:

sch.Rates = updatedVotes;

Rates 是 ICollection&lt;Review&gt;,而 updatedVotes 是 string。您不能将 string 分配给 ICollection&lt;Review&gt;

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多