views:

55

answers:

1

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?

+6  A: 

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"); 
rossisdead
... or sh.Count, of course :)
IanR
@IanR: `IQueryable` doesn't have a `Count` property. `Any()` is the correct answer.
280Z28
@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 :)
IanR