无法将类型为“System.Collections.Generic.List”的对象强制转换为System.Collections.Generic.IList

时间:2013-11-30 07:32:43

标签: asp.net-mvc vb.net linq generics

我有以下代码:

 public class ComputeOperationData
{
    public COUNTERS_DATA CounterData { get; set; }
    public COMPUTES_OPERATION ComputeOperation { get; set; }
    public COUNTERS_FORMULA CounterFormula { get; set; }

}

Public Function GetComputeOperationData(ByVal computeId As Integer, ByVal priodType As Integer, ByVal lstDuringTime As DuringTime) As IList(Of ComputeOperationData) Implements IComputeResultService.GetComputeOperationData
        Dim computeOperations As IQueryable(Of COMPUTES_OPERATION) = _repository.GetObjectQuery(Of COMPUTES_OPERATION).Include("COMPUTES").Include("COUNTERS")
        computeOperations = computeOperations.Where(Function(a) a.COMPUTE_ID = computeId)

        Dim countersFormula As IQueryable(Of COUNTERS_FORMULA) = _repository.GetObjectQuery(Of COUNTERS_FORMULA)().Include("COUNTERS")

        Dim countersData As IQueryable(Of DomainClasses.COUNTERS_DATA) = _repository.GetObjectQuery(Of DomainClasses.COUNTERS_DATA)().Include("COUNTERS")
        countersData = countersData.Where(Function(a) a.PERIOD_TYPE = priodType And a.COUNTER_DATE = lstDuringTime.StartDate And a.COUNTER_TIME >= lstDuringTime.StartTime And a.COUNTER_TIME <= lstDuringTime.EndTime)

                    Dim lstComputeOperationData = From counterData In countersData
                 Join computeOperation In computeOperations On counterData.COUNTER_ID Equals computeOperation.COUNTER_ID
                 Join counterFormula In countersFormula On counterData.COUNTER_ID Equals counterFormula.COUNTER_ID
                 Where counterData.COUNTER_DATE >= counterFormula.FROM_DATE And counterData.COUNTER_DATE <= counterFormula.TO_DATE




        Return lstComputeOperationData.ToList

    End Function

我收到了这个错误:

  

无法转换类型为'System.Data.Objects.ObjectQuery 1[VB$AnonymousType_1 3 [Danesh.Ems.DomainClasses.COUNTERS_DATA,Danesh.Ems.DomainClasses.COMPUTES_OPERATION,Danesh.Ems.DomainClasses.COUNTERS_FORMULA]]'的对象输入'System.Collections.Generic.IList`1 [Danesh.Ems.Models.ComputeOperationData]'

请帮帮我

3 个答案:

答案 0 :(得分:0)

我使用VB.NET已经有一段时间了,但是在C#中,你需要像......这样的东西......

Select New ComputeOperationData() 
{ 
   CounterData = counterData, ComputeOperation = computeOperation, CounterFormula = counterFormula 
}

...在查询结束时将三个独立的数据源转换为单个对象。

答案 1 :(得分:0)

你有返回IList(Of ComputeOperationData)的功能但在你内部返回List(Of AnonymousClass)
所以在你的linq查询中你需要指定你想要返回的对象类型,比如这个

Dim lstComputeOperationData = From counterData In countersData
                              Join computeOperation In computeOperations On counterData.COUNTER_ID Equals computeOperation.COUNTER_ID
                              Join counterFormula In countersFormula On counterData.COUNTER_ID Equals counterFormula.COUNTER_ID
                              Where counterData.COUNTER_DATE >= counterFormula.FROM_DATE And counterData.COUNTER_DATE <= counterFormula.TO_DATE
                              Select New ComputeOperationData With {.CounterData = counterData, .ComputeOperation = computeOperation, .CounterFormula = counterFormula}

答案 2 :(得分:0)

当您看到运行时错误消息:无法将Generic.List [SomeDerivedType]类型的对象强制转换为Generic.IList [SomeBaseType] 时,您可能只需要强制转换在对IList进行赋值之前,将对象转换为列表中的正确类型。

例如:

Dim objectIList As IList(Of Object)
Dim stringList As New List(Of String)
objectIList = stringList.Cast(Of Object).ToList()

这很棘手,因为编译器没有抓住这个。有几个基于C#的问题与此问题有关。这是one。这是another