Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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# 如果输入为null,我可以从LINQ语句返回null吗?_C#_Linq - Fatal编程技术网

C# 如果输入为null,我可以从LINQ语句返回null吗?

C# 如果输入为null,我可以从LINQ语句返回null吗?,c#,linq,C#,Linq,我有这段代码,但当示例为空时,它会给出一个错误。如果result.examples=null,是否有办法将其保留为一行LINQ并将Samplementes设置为null var sampleSentences = result.examples .Select(ex => new SampleSentence { Text = ex }) .ToList(); 您可以使用C6功能: 这将仅在result.examples不为null时

我有这段代码,但当示例为空时,它会给出一个错误。如果result.examples=null,是否有办法将其保留为一行LINQ并将Samplementes设置为null

 var sampleSentences = result.examples
             .Select(ex => new SampleSentence { Text = ex })
             .ToList();
您可以使用C6功能:

这将仅在result.examples不为null时执行选择。

您可以使用C6功能:

仅当result.examples不为空时,才会执行选择。

是,只需使用:

var sampleSentences = result.examples?
             .Select(ex => new SampleSentence { Text = ex })
             .ToList();
是的,只需使用:

var sampleSentences = result.examples?
             .Select(ex => new SampleSentence { Text = ex })
             .ToList();

如果您使用的是C6,则可以编写:

var sampleSentences = result?.examples
         ?.Select(ex => new SampleSentence { Text = ex })
         .ToList();
var sampleSentences = 
         result.examples == null 
             ? null 
             : result.examples
                 .Select(ex => new SampleSentence { Text = ex })
                 .ToList();
否则你可以写:

var sampleSentences = result?.examples
         ?.Select(ex => new SampleSentence { Text = ex })
         .ToList();
var sampleSentences = 
         result.examples == null 
             ? null 
             : result.examples
                 .Select(ex => new SampleSentence { Text = ex })
                 .ToList();

如果您使用的是C6,则可以编写:

var sampleSentences = result?.examples
         ?.Select(ex => new SampleSentence { Text = ex })
         .ToList();
var sampleSentences = 
         result.examples == null 
             ? null 
             : result.examples
                 .Select(ex => new SampleSentence { Text = ex })
                 .ToList();
否则你可以写:

var sampleSentences = result?.examples
         ?.Select(ex => new SampleSentence { Text = ex })
         .ToList();
var sampleSentences = 
         result.examples == null 
             ? null 
             : result.examples
                 .Select(ex => new SampleSentence { Text = ex })
                 .ToList();

另一种方法是使用三元运算符

var sampleSentences = result.examples != null ? result.examples
             .Select(ex => new SampleSentence { Text = ex })
             .ToList() : null;

另一种方法是使用三元运算符

var sampleSentences = result.examples != null ? result.examples
             .Select(ex => new SampleSentence { Text = ex })
             .ToList() : null;

我建议Samplementes是空输入上的空序列。。它打破了要求的契约,但空序列通常更易于传播。我建议Samplementes是空输入上的空序列。。它违反了要求的合同,但空序列通常更易于传播。如果您确信只有示例可以为空,则只需在示例后添加一个问号。@SebastianSchulz您完全正确-很好的点如果您确信只有示例可以为空,则只需在示例后添加一个问号。@SebastianSchulz您完全正确-很好的点如果result.examples为null,则返回null重要!如果result.examples为null,则返回null!