我正在使用下面的LINQ查询,现在我想如果其他条件在LINQ查询中像下面一样-我怎么能实现同样的?
if(stemming)
highlightedText = c.Value.p_content != null && c.Value.p_content[0] != null ? c.Value.p_content[0] : string.Empty
if(phoentic)
highlightedText = c.Value.s_content != null && c.Value.s_content[0] != null ? c.Value.s_content[0] : string.Empty
if(content)
highlightedText = c.Value.content != null && c.Value.content[0] != null ? c.Value.content[0] : string.Empty完整的代码-
var highlightedDataLst = objJson.highlighting.Select(c =>
new finalOutput
{
highlightedKey = c.Key,
highlightedText = c.Value.content != null && c.Value.content[0] != null ? c.Value.content[0] : string.Empty
}).ToList<finalOutput>();发布于 2018-10-30 23:11:01
嗯,尝试一下,它看起来可能是这样的:
var highlightedDataLst = objJson.highlighting.Select(c =>
new finalOutput
{
highlightedKey = c.Key,
highlightedText = (stemming ? c.Value.p_content?[0] :
(phoentic ? c.Value.s_content?[0] :
(content ? c.Value.content?[0] : null))) ?? ""
}).ToList<finalOutput>();可以使用空条件运算符(?[])和空合并运算符(?)简化条件。
https://stackoverflow.com/questions/53074066
复制相似问题