开发者

Cannot implicitly convert type

I have the following function

public Dictionary<DateTime, object> GetAttributeList(
    EnumFactorType attributeType,
    Thomson.Financial.Vestek.Util.DateRange da开发者_C百科teRange)
{
    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?)

Using C# 3.0


It's difficult to know what you really want without more information on your structures. What should the return object be - the asset attribute or the time series? If it's the attribute then something like this should work

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

or if it's just the time series then

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

I'm not a LINQ guru so there may be better ways to do this but these ought to compile at least.

[edit] just spotted your function name: I guess you want the first one then.


the return type of

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

is IEnumerable but the function should return Dictionary


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

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜