2010-05-27 16 views
0

J'ai la fonction suivanteImpossible de convertir implicitement le type

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

en C# 3.0

Répondre

3

Il est difficile de savoir ce que vous voulez vraiment sans plus d'informations sur vos structures. Quel devrait être l'objet de retour - l'attribut asset ou la série chronologique? Si c'est l'attribut alors quelque chose comme cela devrait fonctionner

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

ou si elle est juste la série chronologique puis

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

Je ne suis pas un gourou LINQ donc il peut y avoir de meilleures façons de le faire, mais ceux-ci devrait compiler au moins.

[edit] J'ai juste repéré votre nom de fonction: je suppose que vous voulez le premier alors.

+0

Merci de fournir une réponse non absurde. +1 – leppie

0

le type de retour de

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

est IEnumerable mais la fonction doit retourner Dictionnaire

+0

toujours je reçois la même erreur. – Newbie

+0

Encore une fois - vous essayez de retourner IEnumerable - mais la fonction devrait retourner Dictionnaire Qu'est-ce que vous avez besoin de la fonction pour retourner? –

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

     } 
Questions connexes