2015-10-20 47 views
0

我正在使用FileHandler实用工具将CSV文件转换为对象(Lore)。在提取数据后,其目的是为数据字段someTemp消除重复项。我正在使用LINQ查询来计算不同的值,但它给出了以下错误: 'object'不包含'someTemp'的定义,并且没有找到接受类型'object'的第一个参数的扩展方法'someTemp' (您是否缺少使用指令或装配参考?)在C#中使用LINQ计算不同值时出现错误

有人可以请帮忙。

FileHelperAsyncEngine engine = new FileHelperAsyncEngine(typeof(Lore)); 
      using (engine.BeginReadFile(filePath)) 
      { 
       var distinct = engine.GroupBy(x => x.someTemp).Select(y => y.Last());enter code here 
       // The engine is IEnumerable 
       foreach (Lore lor in engine) 
       { 
        StringBuilder str = new StringBuilder(); 
        str.Append(lor.RowNumber + " "); 
        str.Append(lor.LocalDate + " "); 
        str.Append(lor.LocalTime); 
        Console.WriteLine("{0} {1} {2}",lor.RowNumber,lor.LocalDate,lor.LocalTime); 
        listBox1.Items.Add(str); 
       } 
      } 

[DelimitedRecord(",")] 
    [IgnoreFirst] 
    public class Lore 
    { 
     public int RowNumber; 

     [FieldConverter(ConverterKind.Date,@"MM/dd/yyyy")] 
     public DateTime LocalDate; 

     [FieldConverter(ConverterKind.Date,"HH:mm:ss")] 
     public DateTime LocalTime; 

     public int Batch_Size; 

     public int someTemp { get; set; } 

    } 

回答

1

如果你在编译时知道你的类型,你应该使用通用版本,否则你将不得不施放结果。

var engine = new FileHelperAsyncEngine<Lore>(); 
0

nVoigt的回答是正确的,通用版本是你需要的,我认为它会为你处理铸件。如果没有,正确的路线是:

var distinct = engine.GroupBy(x => x.someTemp).Select(y => (y.Last()) as Lore)

var distinct = engine.GroupBy(x => x.someTemp).Select(y => y.Last()).Cast<Lore>()

,才应使用这种结构,如果你不知道在编译时的类型,然后你会将Lore替换为类型参数T

相关问题