2011-08-24 82 views
4

我有以下错误信息:“不包含定义...并没有扩展方法。”错误

'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?) 

我SO检查这一点,我发现this问题这似乎也有类似的(如果不相同)问题,我有。但是,我尝试了接受答案中提供的解决方案,但仍然没有提出任何解决方案。这就像我错过了一个使用声明,但我几乎积极,我有我需要的所有使用。

下面是一些正在生产中的错误代码:

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; } 
     ... 
    } 
} 

很抱歉,如果我的变量和诸如此类的东西重命名是混乱的。如果它不可读,或者太难遵守,或者其他信息与解决方案有关,请告诉我。谢谢!

回答

6

问题是这个零件:cart.biffcart类型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; 
} 
4

您需要访问字典的价值对于一个给定的关键。沿着这些线路的东西。

foreach(var item in dbContext.DBView) 
{ 
    foreach(var key in cart.Keys) 
    { 
     cart[key].biff = item.biff; 
    } 
} 
+0

该代码将不会编译为车[键]的类型SpoofClass的而item.biff是int。 –

+0

@Ben:我刚刚修好了 –

+0

@你说得对。感谢编辑Daniel –

相关问题