Невозможно ввести приведение <AnonymousType # 1> к <WindowsFormsApplication1.Attributes> [C # 3.0] - PullRequest
0 голосов
/ 19 мая 2010

у меня

 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

Я понял значение ошибки, но как печатать ее. Я использовал var, а затем перебрал его и получил результат. Но без этого каким-либо другим способом, которым я могу это сделать?

Использование C # 3.0

Спасибо

Ответы [ 2 ]

0 голосов
/ 19 мая 2010

В основном сохраните его в анонимный тип списка, а затем вызовите функцию преобразования.

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 голосов
/ 19 мая 2010

может быть:

 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. Но они бывают разных типов и не могут быть привязаны друг к другу.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...