Я использую ниже LINQ Query, теперь я хочу, если еще условие внутри LINQ Query, как below-, как я могу добиться того же?
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>();
Хм, принимая удар, это может выглядеть примерно так:
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>();
Вы можете упростить свои условия, используя нулевой условный оператор (? []) И нулевые операторы слияния (??).
if(stemming)
всегда перезаписывается следующим оператором if / else.