开发者

Unable to type cast <AnonymousType#1> to <WindowsFormsApplication1.Attributes> [ C#3.0 ]

I have

 List<Attributes> la = new List<Attributes>();
la = (from t in result
                 let t1 = t.AttributeCollection
                 from t2 in t1
                 where t2.AttributeCode.Equals(attributeType)
                 let t3 = t2.TimeSeriesData
                 from k in t3.ToList()
                 where k.Key.Equals(startDate) && k.Key.Equals(endDate)
                 select new
                 {
                     AttributeCode = attributeType,
                     TimeSeriesData = fn(k.Key, k.Value.ToString())
                 }).ToList<Attributes>();

I am getting the error:

'System.Collections.Generic.IEnumerable<AnonymousType#1>开发者_运维知识库;' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments

I understood tye error meaning but how to type cast it. I have used var and then iterating over it got the result. But without that any other way by which I can do it?

Using C# 3.0

Thanks


may be:

 List<Attributes> la = new List<Attributes>();
la = (from t in result
                 let t1 = t.AttributeCollection
                 from t2 in t1
                 where t2.AttributeCode.Equals(attributeType)
                 let t3 = t2.TimeSeriesData
                 from k in t3.ToList()
                 where k.Key.Equals(startDate) && k.Key.Equals(endDate)
                 select new Attributes()
                 {
                     AttributeCode = attributeType,
                     TimeSeriesData = fn(k.Key, k.Value.ToString())
                 }).ToList<Attributes>();

you are constructing anonymous type with same fields as Attributes. But they are different types and can't be cast to each other.


Basically save it to an anonymous list type, and then call a conversion function.

public void TestMethod(){
  List<Attributes> la = new List<Attributes>();


  var annonyType = (from t in result
             let t1 = t.AttributeCollection
             from t2 in t1
             where t2.AttributeCode.Equals(attributeType)
             let t3 = t2.TimeSeriesData
             from k in t3.ToList()
             where k.Key.Equals(startDate) && k.Key.Equals(endDate)
             select new
             {
                 AttributeCode = attributeType,
                 TimeSeriesData = fn(k.Key, k.Value.ToString())
             });

   la = annonyType.ConvertAll(x=>ConvertToAttribute(x.AttributeCode , x.TimeSeriesData ));

   ....
 //End test method
 }

  //... (where TimeSeriesDataType  = type returned by fn( 
  public Attributes ConvertToAttribute(string AttributeType, TimeSeriesDataType d){
     return new Attribute()....;

   }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜