What returns empty IQueryable, as if empty?

I need to determine if the IQueryable Method returns with Data or "Empty" when applied to a RadGrid data source, for example:

RadGrid.DataSource = Method(x);

        if (Method(x) == yyy)
        {
            button.Enabled = true;
        }
        else
        {
            button.Enabled = false;
        }

I tried using "zero" instead of "yyy", but without success. When you go through the code, the IQueryable Method returns as "Empty", but I'm not sure how to verify that using the If statement.

What does the IQueryable method return, as if it were returned as Empty, and how can I check that using the If statement?

+5
source share
4 answers

You can use Any () to check for any elements in IQueryable:

RadGrid.DataSource = Method(x);

if (Method(x).Any())
{
    button.Enabled = true;
}
else
{
    button.Enabled = false;
}

(Or, conversely, a shorter version :)

button.Enabled = Method(x).Any();
+12

IQueryable.Any.

bool empty = !queryable.Any();
if(empty) {
    // something
}
+5

-

RadGrid.DataSource = Method(x);            
 if (RadGrid.DataSource as MyObject == null)          
 {              
      button.Enabled = true;          
 }          
 else          
 {              
       button.Enabled = false;          
 }  
0

:

if (Method(x) == Enumerable.Empty<YourType>())
{
    // Your code
}

Since it is Enumerable.Empty<TResult>()cached for TResult, it will contain the same link as the empty sequence remaining from Methodand therefore will be equivalent.

If you don't know if this will work, run this example:

using System;
using System.Linq;

class Example
{
    static void Main()
    {
        var first = Enumerable.Empty<Example>();
        var second = Enumerable.Empty<Example>();

        Console.WriteLine(object.ReferenceEquals(first, second));
    }
}
-2
source

All Articles