如何检查LINQ to SQL查询的结果?

rem*_*rem 3 c# wpf linq-to-sql

在WPF应用程序中,我想检查LINQ to SQL查询的返回是否包含一些记录,但我的方法不起作用:

        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");
        }
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

ros*_*ead 8

linq查询永远不会为null,因为它总是返回一个IQueryable.试着sh.Any()改为打电话.

if (!sh.Any())
    MessageBox.Show("There are no Selected Items"); 
Run Code Online (Sandbox Code Playgroud)