无法将<anonymoustype#1>转换为<windowsformsapplication1.attributes> [C#3.0] </windowsformsapplication1.attributes> </anonymoustype#1>

时间:2010-05-19 12:50:22

标签: c#-3.0

我有

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

我收到错误:

'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

我理解tye错误含义但是如何输入它。我使用了var然后迭代它得到了结果。但是没有其他任何方式我可以做到这一点?

使用C#3.0

由于

2 个答案:

答案 0 :(得分:0)

可能是:

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

您正在使用与Attributes相同的字段构建匿名类型。但它们是不同的类型,不能相互铸造。

答案 1 :(得分:0)

基本上将其保存为匿名列表类型,然后调用转换函数。

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

   }