C# Linq,排除SQL空值

C# Linq,排除SQL空值,c#,linq,C#,Linq,我正在生成一个饼图,目前我有以下变量,如何排除空记录?它可以工作,但在饼图中包含空值 var PieChartData1 = from T1 in Result group T1 by T1.Reasons into G1 orderby count ascending select new { G1.Key, Count = G1.Count() }; 在使用wher

我正在生成一个饼图,目前我有以下变量,如何排除空记录?它可以工作,但在饼图中包含空值

var PieChartData1 = from T1 in Result
                    group T1 by T1.Reasons  into G1
                    orderby count ascending
                    select new { G1.Key, Count = G1.Count()  };


在使用
where
子句之前,添加
where
子句以过滤
null
值-这里我假设
T1
T1。原因可以是
null

var PieChartData1 = from T1 in Result
                        where T1 != null && T1.Reasons != null
                        group T1 by T1.Reasons into G1
                        orderby count ascending
                        select new { G1.Key, Count = G1.Count() };

我还怀疑
orderby count ascending
应该是
orderby G1.count()ascending
如果T1为空,请使用HasValue属性

var PieChartData1 = from T1 in Result
                        where T1.HasValue
                        group T1 by T1.Reasons into G1
                        orderby count ascending
                        select new { G1.Key, Count = G1.Count() };

也可以使用lambda表达式编写:

var pieChartData = Result.Where(r => r.Reason != null)
                         .GroupBy(r => r.Reason)
                         .OrderBy(g => g.Count())
                         .Select(g => new { g.Key, Count = g.Count() });

添加Where子句以筛选出空Where原因!=无效的我得到一个错误(原因在当前上下文中不存在),它不应该是
where T1.Reasons!=空值
?@Rufus L,这很有效!非常感谢。事实上,我用了>>哪里T1.原因!=谢谢!!事实上,我用了>>哪里T1.原因!=null…>>其中T1.HasValue()似乎不起作用
var pieChartData = Result.Where(r => r.Reason != null)
                         .GroupBy(r => r.Reason)
                         .OrderBy(g => g.Count())
                         .Select(g => new { g.Key, Count = g.Count() });