【问题标题】:What wrong I am doing in IEnumerable(C#3.0)我在 IEnumerable(C#3.0) 中做错了什么
【发布时间】:2010-06-04 15:25:55
【问题描述】:

如果我写

var v = (from r in stock.ReplacementLog
                                             select new 
                                             {
                                                 AssetId = stock.AssetId,
                                                 Date = stock.ReferDate,
                                                 FactType = r.Key,
                                                 Value = r.Value
                                             });

一切正常……

如果我这样做了

IEnumerable<StockAsset> v = (from r in stock.ReplacementLog
 select new  {
 AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value });

我收到错误:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(您是否缺少演员表?)

然后我做了

IEnumerable<StockAsset> v = 
(
from r in stock.ReplacementLog
select new
{
AssetId = stock.AssetId,
Date = stock.ReferDate,
FactType = r.Key,
Value = r.Value
}).ToList<StockAsset>();

出现以下一堆错误:

错误 1 ​​实例参数:无法从 'System.Collections.Generic.IEnumerable' 转换为 'System.Collections.Generic.IEnumerable'

错误 2“System.Collections.Generic.IEnumerable”不包含“ToList”的定义和最佳扩展方法重载“System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)”有一些无效的参数

然后我尝试了

IEnumerable<StockAsset> v1 = 
(from r in stock.ReplacementLog
select new StockAsset 
{
AssetId = stock.AssetId,
ReferDate= stock.ReferDate,
FactType = r.Key,
Value = r.Value
}); 

有错误: 错误 1 “StockAsset”不包含“FactType”的定义

**Error 2
'StockAsset' does not contain a definition for Value'** 

StockAsset 类如下

public class StockAsset
{
        public int AssetId { get; set; }
        public DateTime ReferDate {get;set;}        
        public Dictionary<EnumFactorType, double> ReplacementLog { get; set; }   
}

需要帮助。

使用 C#3.0

谢谢

【问题讨论】:

    标签: linq c#-3.0 anonymous-types


    【解决方案1】:

    当你写作时

    select new  {
     AssetId = stock.AssetId,
     Date = stock.ReferDate,
     FactType = r.Key,
     Value = r.Value }
    

    您实际上生成了一个匿名类型。您不能将此匿名类型强制转换为已声明的类型。

    如果你想创建你应该做的类的对象

     select new StockAsset
     {
         AssetId = ..., // Maybe stock.AssetId
         ReferDate = ..., // Maybe stock.ReferDate
         ReplacementLog = ... // Maybe new Dictionary<string, short> { {r.Key, r.Value} };
     }
    

    【讨论】:

    • 不幸的是,StockAsset 类没有 Date、FactType 或 Value 属性,所以仍然会报错。
    • 其实是的...那么如何克服这一点
    • 错误 1“StockAsset”不包含“Date”定义错误2“StockAsset”不包含“FactType”定义错误3“StockAsset”不包含“值”定义
    • @Newbie,您应该决定如何为 StockAsset 的属性创建值。
    • 先生,我使用 VAR 生成的输出(如问题中所述)
    【解决方案2】:

    当你写作时

    select new { property = value }
    

    您正在创建一个新的 anonymous 类型的实例,而看起来您实际上想要创建 StockAssets,所以您想要

    select new StockAsset { property = value }
    

    【讨论】:

    • 我收到错误:错误 1“StockAsset”不包含“日期”的定义 错误 2“StockAsset”不包含“FactType”的定义 错误 3“StockAsset”不包含当我在做 IEnumerable v1 = (from r in stock.ReplacementLog select new StockAsset { AssetId = stock.AssetId, Date = stock.ReferDate, FactType = r.Key, Value = r.Value }) ;
    • StockAsset 没有名为 Value 的属性。您是否真的在尝试创建 StockAsset 对象,如果是,那么 Value 应该是什么?
    【解决方案3】:

    我的最佳猜测是您正在尝试为另一个 StockAsset 的 ReplacementLog 中的每个条目创建一个新的 StockAsset。这个新的 StockAsset 将在其 ReplacementLog 中有一个条目。这个对吗?如果是这样,您可以在 StockAsset 上创建一个新的构造函数:

    public StockAsset(int assetId, DateTime referDate, 
                      EnumFactorType factorType, double value)
    {
        AssetId = assetId;
        ReferDate = referDate;
        ReplacementLog = new Dictionary<EnumFactorType, double>();
        ReplacementLog[factorType] = value;
    }
    

    然后在 LINQ 中调用该构造函数。

    IEnumerable<StockAsset> v = (from r in stock.ReplacementLog
        select new StockAsset(stock.AssetId, stock.ReferDate, r.Key, r.Value));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多