匿名类型,从if语句强制转换为AsQueryable,然后查询

时间:2011-11-02 07:46:15

标签: vb.net linq entity-framework

我有这个if语句,它将结果作为AsQueryable返回给匿名类型:

If (signature = "") Then
                testResults = (From TRTable In context.TestResults
                                        Where ((TRTable.Art_no = currentProduct) And (TRTable.Server_time > startDate) And (TRTable.Server_time < endDate))
                                        Select TRTable.Art_no, TRTable.Failed, TRTable.Retested).AsQueryable
            ElseIf (signInfo = "out") Then
                testResults = (From TRTable In context.TestResults
                                        Where ((TRTable.Art_no = currentProduct) And (TRTable.Server_time > startDate) And (TRTable.Server_time < endDate) And TRTable.Sign <> signature)
                                        Select TRTable.Art_no, TRTable.Failed, TRTable.Retested).AsQueryable
            Else
                testResults = (From TRTable In context.TestResults
                                        Where ((TRTable.Art_no = currentProduct) And (TRTable.Server_time > startDate) And (TRTable.Server_time < endDate) And TRTable.Sign = signature)
                                        Select TRTable.Art_no, TRTable.Failed, TRTable.Retested).AsQueryable
            End If

然后当我做

StatisticsModel.Passed = testResults.Where(Function(p) p.Failed = 0).Count

我得到了

Method invocation failed because 'Public Function Where(predicate As String, ParamArray parameters As System.Data.Objects.ObjectParameter()) As System.Data.Objects.ObjectQuery`1[[VB$AnonymousType_3`3[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable(Of VB$AnonymousType_3(Of String,Nullable(Of Decimal),Nullable(Of Decimal)))' cannot be called with these arguments:
Argument matching parameter 'predicate' cannot convert from 'VB$AnonymousDelegate_1(Of Object,Object)' to 'String'.

很抱歉文本很长,但我似乎无法弄清楚它为什么不起作用,它没有if语句,但我需要这个功能。 谢谢:))

1 个答案:

答案 0 :(得分:3)

您正在尝试使用System.Linq.Queryable.Where扩展方法,但您的代码认为您需要System.Data.Objects.ObjectQuery.Where

确保文件顶部有正确的Imports System.Linq声明。如果这不起作用,您可能需要直接致电System.Linq.Queryable.Where(testResults, ...).Count。您也可以使用Count(predicate)代替Where(predicate).Count,这也可以避免冲突。