2010-05-27 104 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);     

     } 
相關問題