views:

23

answers:

2

I have been looking everywhere and can not seem to solve this. I have the following linq to data entities that sometime returns an empty set. But I can not seem to test for the empty set?

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

Everytime I try this it evaluates to false whether or not there are entries in the Result set?

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

If I try to use xrefLineItems.Any() I get the follow exception

The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.

cheers bob

Here is the stack trace

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
A: 

you can use Count

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

if (xrefLineItems.Count() == 0)
{
  // empty
}
fampinheiro
I tried that in the very beginning and recieved the NotSupported exception about The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead. Evidently the Count() method calls first just like the .Any() method seems to.
Bob Cummings
No. [Don't use Count when you mean Any.](http://blogs.teamb.com/craigstuntz/2010/04/21/38598/) Neither `Count` nor `Any` call `First`. His question is misleading.
Craig Stuntz
A: 

Any() is the correct method.

Neither Any() nor Count() call First(). If you are seeing this, then your bug is elsewhere. Look at the call stack for the exception.

Craig Stuntz
OK I posted the stack trace. I am not the best at reading them, so if you can show me where the bug is generated, I would appreciate it very much.
Bob Cummings
All that stack says is that you have a `First()` in some query you're not showing us. The EF is trying to translate it to SQL, but it's not used correctly. The error is in code which is not in your post.
Craig Stuntz
Thanks, the only code is being show, where the query is done (first code section) then when I try to check to see if there were any results returned. I just wrote a stored procedure and call that instead of trying to build the query (shown in the first code section) and then the .ALL works.thanks again for your effortbob
Bob Cummings
I know that's the only code you *see*, but with lazy evaluation it's likely not the only code which *runs.*
Craig Stuntz
Thanks for tip about lazy evaluation. Any hints on where I can learn more about this concept? Otherwise I can just Google it, and work it out myself. I feel as if I am "Programming by Coincidence". And need to get beyond that, and fully understand what is going on, for my own piece of mind and to be able to ask better questions and understand the answers. thanks again for everything.cheersbob
Bob Cummings
You could do worse than Googling LINQ + lazy. But the basic idea is that no query is executed or even translated to SQL until it *has to* be evaluated. `.Any()` forces translation to SQL. Errors elsewhere in the code won't surface until that point. Search your code for `First()`; it should never appear *inside* an L2E query; you can only apply it to the whole query itself.
Craig Stuntz