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

时间:2013-03-21 11:30:51

标签: c# linq null anonymous

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

我想用我的匿名类型的结果填充我的Gridview。 “循环”中的第一次运行顺利,因为所有变量都有一些实际值。

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

这是因为我可能将一个匿名对象指定为null并且gridview无法处理它。 我试图找出一些解决方案,但还没有解决。

是否有正确理解LinQ的秘密通道?

这是我现在的代码:

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();

3 个答案:

答案 0 :(得分:4)

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

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 ),
};

主要思想不是混淆,也不是使用功能方法使您的解决方案复杂。

答案 1 :(得分:0)

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

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

info.WrongAnswer3相同。

答案 2 :(得分:0)

您可能正在检查错误的属性,请在检查info.WrongAnswer2之前尝试检查info.WrongAnswer3OptionText是否为空。