检查从linq到数据实体查询的空结果

时间:2010-08-31 21:16:38

标签: linq-to-entities

我一直在寻找,似乎无法解决这个问题。我有以下linq to data实体,有时会返回一个空集。但我似乎无法测试空集?

           IEnumerable<string> xrefLineItems = from xref in db.wysLkupItemCrossRefs
                                            where xref.EndPointCustID == inCustID
                                            select xref.BuyersItemNo;  

每当我尝试这个时,它会评估为假,结果集中是否有条目?

        if (xrefLineItems == Enumerable.Empty<string>())
        {  }

如果我尝试使用xrefLineItems.Any(),我会得到以下异常

方法'First'只能用作最终查询操作。请考虑在此实例中使用方法“FirstOrDefault”。

欢呼声 鲍勃

这是堆栈跟踪

Line 146:            
Line 147:            bool isEmpty;
Line 148:            if (xrefLineItems.Any())
Line 149:            {
Line 150:                isEmpty = true;


Source File: H:\DirectEDI\MVC_EDI\MVC_EDI\MVC_EDI\Controllers\DirectEDIController.cs    Line: 148

Stack Trace:

[NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.]
   System.Data.Objects.ELinq.FirstTranslator.TranslateUnary(ExpressionConverter parent, DbExpression operand, MethodCallExpression call) +100
   System.Data.Objects.ELinq.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +81
   System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +14
   System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +102
   System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
   System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
   System.Data.Objects.ELinq.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) +70
   System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
   System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
   System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) +80
   System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding) +88
   System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) +85
   System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +37
   System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +14
   System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +102
   System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
   System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
   System.Data.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) +49
   System.Data.Objects.ELinq.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) +37
   System.Data.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) +14
   System.Data.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) +102
   System.Data.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) +54
   System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +110
   System.Data.Objects.ELinq.ExpressionConverter.Convert() +16
   System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +110
   System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +149
      System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +44
   System.Linq.Enumerable.Any(IEnumerable`1 source) +71
   MVC_EDI.Controllers.DirectEDIController.hasTrakNumber(Guid inOrderID) in H:\DirectEDI\MVC_EDI\MVC_EDI\MVC_EDI\Controllers\DirectEDIController.cs:148

2 个答案:

答案 0 :(得分:2)

Any()是正确的方法。

Any()Count()都不会拨打First()。如果您看到这个,那么您的错误就在其他地方。查看异常的调用堆栈。

答案 1 :(得分:0)

您可以使用Count

IEnumerable<string> xrefLineItems = from xref in db.wysLkupItemCrossRefs
                                    where xref.EndPointCustID == inCustID
                                    select xref.BuyersItemNo;  

if (xrefLineItems.Count() == 0)
{
  // empty
}