Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Linq表达式中使用可为空的类型_C#_Linq_Nullable - Fatal编程技术网

C# 在Linq表达式中使用可为空的类型

C# 在Linq表达式中使用可为空的类型,c#,linq,nullable,C#,Linq,Nullable,在我的select表达式中,我想在表达式的最后一部分选择一个可为null的类型,但显然我无法用我目前尝试的方式来完成 如何将WAS正确设置为可空类型 我试过了?是正确的,但在Visual Studio中也不会出现错误 您需要显式地将null值强制转换为可为null的类型: var quantSubset = from userAns in userAnalysis.AllUserAnswers join ques in userAnalysis.AllSeenQuestions

在我的select表达式中,我想在表达式的最后一部分选择一个可为null的类型,但显然我无法用我目前尝试的方式来完成

如何将WAS正确设置为可空类型

我试过了?是正确的,但在Visual Studio中也不会出现错误

您需要显式地将null值强制转换为可为null的类型:

var quantSubset =
    from userAns in userAnalysis.AllUserAnswers
    join ques in userAnalysis.AllSeenQuestions on userAns.QID equals ques.QID
    where (ques.QuestionType == "QT")
    select new {
        QuestionLevel = ques.LevelID,
        TimeTaken = userAns.TimeTaken,
        Points = userAns.Points,
        UsedWeapon = (userAns.UsedBy2 && userAns.UsedHint),
        WasCorrect = userAns.WasCorrect.HasValue ? userAns.WasCorrect.Value : null
    };
否则C将不知道条件表达式应该是哪种类型

除此之外,代码是完全冗余的。你可以简单地写:

WasCorrect = userAns.WasCorrect.HasValue ?
    userAns.WasCorrect.Value : (TheTypeName?)null

你绝对必须会写作

WasCorrect = userAns.WasCorrect
如果userAns.WasCorrect为空

此代码执行时没有问题:

select new { WasCorrect = userAns.WasCorrect }

您遇到的实际错误是什么?WasCorrect=userAns.WasCorrect不工作吗?不,如果我这样做,我会得到以下错误?WasCorrect语法错误,expected@RPM:编译器需要推断类型时执行此操作。e、 例如,var val=null//空什么?@Konrad嗯,我以前写过代码,就像你说的那样,但是每当遇到空值时就会抛出异常。查看@sassyboy的详细信息:我应该在您链接的页面上查看哪些详细信息?我编写的代码从不抛出异常。您可能错误地编写了WasCorrect=userAns.WasCorrect.Value。有消息告诉我,当值为null时,他试图显式地将其转换为bool。var b=boolx.WasCorrect;或者随便什么类型的is@konrad好啊我的错。。几天前,我编写了一个if条件if userAns.WasCorrect,当值为null时出现错误,因此在此之后,我一直在检查userAns.WasCorrect.HasValue的所有位置。让我试试你指出的。是的,当然。我只是为了证明代码的正确性才发布了这个。
class Test {
    public bool? NullableBool { get; set;}
}

class MainClass
{
    public static void Main ()
    {
        Test t1 = new Test { NullableBool = true };
        var a1 = new { NB = t1.NullableBool };

        Test t2 = new Test { NullableBool = null };
        var a2 = new { NB = t2.NullableBool };
    }
}