匿名类型LinQ-检查空值-异常

本文关键字:空值 异常 检查 LinQ- 类型 | 更新日期: 2023-09-27 18:05:10

我已经在谷歌上搜索了一段时间,并尝试了各种类型的组合来解决我的问题。

我想用匿名类型的结果填充我的网格视图。"循环"中的第一次运行很顺利,因为所有变量都有一些实际值。

但第二次填充第2行时抛出:对象引用未设置为对象的实例

这是因为我可能将一个匿名对象分配给null,而gridview无法处理它。我试着想出一些解决办法,但什么都没有解决。

有没有一个秘密通道可以正确理解林?

这是我现在的代码:

var filteredlist = from info in list // List from external
                   select new
                   {
                       Question = info.QuestionText ?? "Test",
                       CorrectAnswer = info.CorrectAnswer.OptionText ?? "Test",
                       WrongAnswer1 = info.WrongAnswer1.OptionText ?? "Test",
                       WrongAnswer2 = info.WrongAnswer2.OptionText ?? "Test", //Throws Error
                       WrongAnswer3 = info.WrongAnswer3.OptionText ?? "Test" //Throws Error
                   };
GridView1.DataSource = filteredlist;
GridView1.DataBind();

这是我试图让它发挥作用。

var filteredlist = from info in list // list from external
                   select new
                   {
                       Question = info.QuestionText == null ? "Test" : info.QuestionText,
                       CorrectAnswer = info.CorrectAnswer.OptionText == null ? "Test" : info.CorrectAnswer.OptionText,
                       WrongAnswer1 = info.WrongAnswer1.OptionText == null ? "Test" : info.WrongAnswer1.OptionText,
                       WrongAnswer2 = info.WrongAnswer2.OptionText == null ?  "Test" : info.WrongAnswer2.OptionText, //Throws Error
                       WrongAnswer3 = info.WrongAnswer3.OptionText == null ? "Test" : info.WrongAnswer3.OptionText, //Throws Error
                   };

GridView1.DataSource = filteredlist;
GridView1.DataBind();

第二次尝试

var filteredlist = from info in list // list from external
                   select new
                   {
                       Question = info.QuestionText != null ? "Test" : info.QuestionText,
                       CorrectAnswer = info.CorrectAnswer.OptionText != null ? "Test" : info.CorrectAnswer.OptionText,
                       WrongAnswer1 = info.WrongAnswer1.OptionText != null ? "Test" : info.WrongAnswer1.OptionText,
                       WrongAnswer2 = info.WrongAnswer2.OptionText != null ? "Test" : info.WrongAnswer2.OptionText, //Throws Error
                       WrongAnswer3 = info.WrongAnswer3.OptionText != null ? "Test" : info.WrongAnswer3.OptionText, //Throws Error
                   };
GridView1.DataSource = filteredlist;
GridView1.DataBind();

匿名类型LinQ-检查空值-异常

尝试检查WrongAnswer s是否不为空,如下所示:

from info in list
where info != null
select new
{
    Question = info.QuestionText ?? "Test",
    CorrectAnswer = info.CorrectAnswer.OptionText ?? "Test",
    WrongAnswer1  = info.WrongAnswer1 !=null ? info.WrongAnswer1.OptionText : "Test",
    WrongAnswer2  = info.WrongAnswer2 !=null ? info.WrongAnswer2.OptionText : "Test", 
    WrongAnswer3  = info.WrongAnswer3 !=null ? info.WrongAnswer3.OptionText : "Test"
};

我喜欢使用通用方法来提高可读性,比如:

public string DefaultIfNull<TValue>(TValue value, Func<TValue, string> selector) where TValue : class
{
    if(value != null)
        return selector(value);
    return "Test"; //you can make resut generic too
}   

现在您可以:

Func<Answer,string> optionTextSelector = answer => answer.OptionText;
from info in list
where info != null
select new
{
    Question = info.QuestionText ?? "Test",
    CorrectAnswer = DefaultIfNull( info.CorrectAnswer, optionTextSelector ),
    WrongAnswer1  = DefaultIfNull( info.WrongAnswer1,  optionTextSelector ),
    WrongAnswer2  = DefaultIfNull( info.WrongAnswer2,  optionTextSelector ),
    WrongAnswer3  = DefaultIfNull( info.WrongAnswer3,  optionTextSelector ),
};

主要思想是不要混淆,也不要使用函数方法使您的解决方案过于复杂。

info.WrongAnswer2很可能是null。尝试:

WrongAnswer2 = info.WrongAnswer2 == null ? "Nothing To See Here" : 
    info.WrongAnswer2.OptionText ?? "Test"

与CCD_ 4类似。

您可能检查了错误的属性,请在检查OptionText之前先检查info.WrongAnswer2info.WrongAnswer3是否为null。