2010-05-27 106 views
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

这是如果没有更多关于结构的信息,很难知道你真正想要什么。回报对象应该是什么 - 资产属性或时间序列?如果它的属性,那么这样的事情应该工作

//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大师所以可能会有更好的方法来做到这一点,但这些至少应该编译。

刚刚发现你的函数名称:我想你想要的第一个。

+0

谢谢你提供一个非无意义的答案。 +1 – leppie 2010-05-27 11:31:59

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的但功能应该返回字典

+0

仍然我得到相同的错误。 – Newbie 2010-05-27 11:08:16

+0

再次 - 您尝试返回IEnumerable - 但该函数应返回字典 您需要什么函数才能返回? – 2010-05-27 11:14:55

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);     

     } 
相关问题