3

In a WPF app I'd like to check if a return of a LINQ to SQL query contains some records, but my approach doesn't work:

        TdbDataContext context = new TdbDataContext();
        var sh = from p in context.Items where p.Selected == true select p;

        if (sh == null)
        {
            MessageBox.Show("There are no Selected Items");
        }

Where am I wrong?

2 Answers 2

8

A linq query will never be null because it will always return an IQueryable. Try calling sh.Any() instead.

if (!sh.Any())
    MessageBox.Show("There are no Selected Items"); 
Sign up to request clarification or add additional context in comments.

2 Comments

@IanR: IQueryable doesn't have a Count property. Any() is the correct answer.
@280Z28 - Sorry, my bad, I was thinking of the Count() extension method, but as someone else pointed out, this would enumerate the sequence.. +1 from me for Any(), anyway :)
1
    var query = (from k in context.invis select k.invoice);
    if (query.Count() > 0)
    {

    }
    else
    {

    }
    //

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.