“”不包含定义,也没有扩展方法.错误
本文关键字:扩展 方法 错误 包含 定义 | 更新日期: 2023-09-27 18:06:37
我有以下错误信息:
'System.Collections.Generic.Dictionary<int,SpoofClass>' does not
contain a definition for 'biff' and no extension method 'biff'
accepting a first argument of type
'System.Collections.Generic.Dictionary<int,SpoofClass>' could be found
(are you missing a using directive or an assembly reference?)
我检查了这个,我发现这个问题似乎有一个类似的(如果不相同)问题,因为我有。然而,我尝试了在公认的答案中提供的解决方案,它仍然没有提出任何东西。它的行为就像我缺少一个using语句,但我几乎肯定我有所有我需要的using。
下面是一些产生错误的代码:
using locationOfSpoofClass;
...
Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
foreach (var item in dbContext.DBView)
{
cart.biff = item.biff;
...
}
SpoofClass文件:
namespace locationOfSpoofClass
{
public class SpoofClass
{
public int biff { get; set; }
...
}
}
抱歉,如果我重命名的变量和什么是混乱的。如果它是不可读的,或太难遵循,或者如果其他信息是相关的解决方案,请让我知道。谢谢!
问题出在这部分:cart.biff
。cart
为Dictionary<int, SpoofClass>
型,而非SpoofClass
型。
我只能猜测你想要做什么,但是编译了以下代码:
Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
int i=0;
foreach (var item in dbContext.DBView)
{
cart.Add(i, new SpoofClass { biff = item.biff });
++i;
}
您需要访问给定键的Dictionary的Value。
foreach(var item in dbContext.DBView)
{
foreach(var key in cart.Keys)
{
cart[key].biff = item.biff;
}
}