为对象属性赋值时进行多重内联比较
Multiple inline comparison while assigning values to object properties
我正在研究 C# 数据模型 class,我创建了实例并从另一个对象分配值。我知道单行比较,但我需要多行比较,我尝试过 && 运算符,但编译器抱怨 'invalid opperator'。我确定我错过了什么,肯定很简单!
var answerDataModel = new AnswerDataModel()
{
Id = answerId,
//need help to
Value = AnswerDtoObject.Answers.FirstOrDefault() == null? &&
isSubQuestionExist == true ? "_SBQA"
: AnswerDtoObject.Answers.FirstOrDefault(),
};
如果我删除“&& isSubQuestionExist == true ?”,则代码有效
Value = (AnswerDtoObject.Answers.FirstOrDefault() == null) &&
(isSubQuestionExist == true) ? "_SBQA"
: AnswerDtoObject.Answers.FirstOrDefault(),
您有一个错误的 ?
操作员。只需将您的比较组合在一个条件运算符中:
Value = (AnswerDtoObject.Answers.FirstOrDefault() == null && isSubQuestionExist == true) ?
"_SBQA" :
AnswerDtoObject.Answers.FirstOrDefault()
我正在研究 C# 数据模型 class,我创建了实例并从另一个对象分配值。我知道单行比较,但我需要多行比较,我尝试过 && 运算符,但编译器抱怨 'invalid opperator'。我确定我错过了什么,肯定很简单!
var answerDataModel = new AnswerDataModel()
{
Id = answerId,
//need help to
Value = AnswerDtoObject.Answers.FirstOrDefault() == null? &&
isSubQuestionExist == true ? "_SBQA"
: AnswerDtoObject.Answers.FirstOrDefault(),
};
如果我删除“&& isSubQuestionExist == true ?”,则代码有效
Value = (AnswerDtoObject.Answers.FirstOrDefault() == null) &&
(isSubQuestionExist == true) ? "_SBQA"
: AnswerDtoObject.Answers.FirstOrDefault(),
您有一个错误的 ?
操作员。只需将您的比较组合在一个条件运算符中:
Value = (AnswerDtoObject.Answers.FirstOrDefault() == null && isSubQuestionExist == true) ?
"_SBQA" :
AnswerDtoObject.Answers.FirstOrDefault()