无法隐式转换类型

时间:2010-05-27 10:51:29

标签: c# c#-3.0

我有以下功能

public Dictionary<DateTime, object> GetAttributeList(
    EnumFactorType attributeType,
    Thomson.Financial.Vestek.Util.DateRange dateRange)
{
    DateTime startDate = dateRange.StartDate;
    DateTime endDate = dateRange.EndDate;            

    return (
        //Step 1: Iterate over the attribute list and filter the records by 
        // the supplied attribute type 
        from assetAttribute in AttributeCollection
        where assetAttribute.AttributeType.Equals(attributeType)

        //Step2:Assign the TimeSeriesData collection into a temporary variable  
        let timeSeriesList = assetAttribute.TimeSeriesData

        //Step 3: Iterate over the TimeSeriesData list and filter the records by 
        // the supplied date  
        from timeSeries in timeSeriesList.ToList()
        where timeSeries.Key >= startDate && timeSeries.Key <= endDate

        //Finally build the needed collection 
        select timeSeries);
}

Error:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<System.DateTime,object>>'
to 'System.Collections.Generic.Dictionary<System.DateTime,object>'.
An explicit conversion exists (are you missing a cast?)

使用C#3.0

3 个答案:

答案 0 :(得分:3)

如果没有关于结构的更多信息,很难知道你真正想要的是什么。返回对象应该是什么 - 资产属性或时间序列?如果它是属性,那么类似的东西应该起作用

//Finally build the needed  collection 
select new {timeSeries.Key, assetAttribute})
    .ToDictionary(t => t.Key, t => (object)t.assetAttribute);

或者如果它只是时间序列那么

//Finally build the needed  collection 
select timeSeries).ToDictionary(t => t.Key, t => (object)t);

我不是LINQ大师,所以可能有更好的方法来做到这一点,但这些应该至少编译。

[edit]刚发现你的功能名称:我想你想要第一个。

答案 1 :(得分:0)

的返回类型
return ((
                //Step 1: Iterate over the attribute list and filter the records by
                // the supplied attribute type
                     from assetAttribute in AttributeCollection
                     where assetAttribute.AttributeType.Equals(attributeType)
                     //Step2:Assign the TimeSeriesData collection into a temporary variable 
                     let timeSeriesList = assetAttribute.TimeSeriesData
                     //Step 3: Iterate over the TimeSeriesData list and filter the records by
                     // the supplied date 
                     from timeSeries in timeSeriesList.ToList()
                     where timeSeries.Key >= startDate && timeSeries.Key <= endDate
                     //Finally build the needed  collection
                     select new AssetAttribute()
                     {
                         AttributeType = assetAttribute.AttributeType
                         ,
                         Periodicity = assetAttribute.Periodicity
                        ,
                         TimeSeriesData = PopulateTimeSeriesData(timeSeries.Key, timeSeries.Value)
                     }).ToList<AssetAttribute>().Select(i => i.TimeSeriesData));

是IEnumerable但该函数应返回Dictionary

答案 2 :(得分:0)

public Dictionary<DateTime, object> GetAttributeList(
            EnumFactorType attributeType
            ,Thomson.Financial.Vestek.Util.DateRange dateRange)
        {
            DateTime startDate = dateRange.StartDate;
            DateTime endDate = dateRange.EndDate;
            return (
                //Step 1: Iterate over the attribute list and filter the records by 
                // the supplied attribute type 
              from assetAttribute in AttributeCollection
              where assetAttribute.AttributeType.Equals(attributeType)
              //Step2:Assign the TimeSeriesData collection into a temporary variable  
              let timeSeriesList = assetAttribute.TimeSeriesData
              //Step 3: Iterate over the TimeSeriesData list and filter the records by 
              // the supplied date  
              from timeSeries in timeSeriesList.ToList()
              where timeSeries.Key >= startDate && timeSeries.Key <= endDate
              select new { timeSeries.Key, timeSeries.Value })
              .ToDictionary(x => x.Key, x => x.Value);                 

        }