How to check LINQ to SQL query results?

In a WPF application, I would like to check if the LINQ to SQL query return contains some entries, but my approach does not 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 mistaken?

+3
source share
2 answers

The linq query will never be zero, as it will always return IQueryable. Try calling instead sh.Any().

if (!sh.Any())
    MessageBox.Show("There are no Selected Items"); 
+8
source
    var query = (from k in context.invis select k.invoice);
    if (query.Count() > 0)
    {

    }
    else
    {

    }
    //
+1
source

Source: https://habr.com/ru/post/1736605/


All Articles