Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an Empty IQueryable return as when Empty?

I am in need of determining whether an IQueryable Method returns with Data, or "Empty" when applying it to a DataSource of a RadGrid like so:

RadGrid.DataSource = Method(x);

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

I have tried using "null" in place of the "yyy" but with no success. When stepping through the code, the IQueryable Method returns as "Empty" but I am unsure of how to verify that using an If statement.

What does an IQueryable Method return as if it returns as Empty, and how can I verify that using an If Statement?

like image 626
Lando Avatar asked Jan 11 '11 20:01

Lando


People also ask

Can IQueryable return null?

IQueryable. DefaultIfEmpty() marked as non-nullable even though it can return null values as a LEFT JOIN · Issue #69817 · dotnet/runtime · GitHub.

What does Linq return when the results are empty?

It will return an empty enumerable. It won't be null.

What is IQueryable return?

IQueryable is executed. // // Returns: // A System.Type that represents the type of the element(s) that are returned when. // the expression tree associated with this object is executed.

What is IQueryable C#?

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider.


2 Answers

You can use Any() to check to see if there are any elements in the IQueryable:

RadGrid.DataSource = Method(x);

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

(Or, alternatively, the shorter version:)

button.Enabled = Method(x).Any();
like image 191
Reed Copsey Avatar answered Sep 20 '22 21:09

Reed Copsey


You want to use IQueryable.Any.

bool empty = !queryable.Any();
if(empty) {
    // something
}
like image 44
jason Avatar answered Sep 21 '22 21:09

jason