gpt4 book ai didi

linq-to-entities - 检查 linq to data 实体查询的空结果

转载 作者:行者123 更新时间:2023-12-02 17:40:56 24 4
gpt4 key购买 nike

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

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

每次我尝试此操作时,无论结果集中是否有条目,它都会评估为 false?

        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

最佳答案

Any() 是正确的方法。

Any()Count() 都不会调用 First()。如果您看到这一点,那么您的错误就在其他地方。查看调用堆栈中是否有异常。

关于linq-to-entities - 检查 linq to data 实体查询的空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com