我有5个存储的变量,我想找到具有最大值的变量名.
我已将此答案用作以下代码的基础.但是我不明白它在做什么,因此不能确定我得到了正确的结果.
p.joy = .6,
p.fear = .5,
p.sadness =.4,
p.disgust =.1,
p.anger =.7,
var emotion = p.joy > p.fear ? p.joy > p.sadness ? p.joy > p.disgust ? p.joy > p.anger ? "Joy" : "Joy" : "Joy" : p.fear > p.sadness ? p.fear > p.disgust ? p.fear > p.anger ? "Fear" : "Fear" : "Fear" : "Fear" : p.sadness > p.disgust ? p.sadness > p.anger ? "Sadness" : "Sadness" : p.disgust > p.anger ? "Disgust" : "Anger"
Run Code Online (Sandbox Code Playgroud)
我希望有人可以验证,或根据需要进行调整.
用例:我想返回具有最高值的变量名,其中有两个或多个变量具有相同的数字我希望按此顺序返回它们.愤怒,恐惧,悲伤,厌恶,喜悦
这是更大的代码段,即linq
var Watson1 = from s in db.tblWatsonToneAnalyser
join e in db.Projects on s.ProjectRef equals e.ID
where e.Deactivate != true && e.Type == part && e.PartitNo == item
group s by new { s.ProjectRef} into grp
select grp.OrderByDescending(v => v.CreatedDate).AsEnumerable().Select(p => new
{
ProjectRef = p.ProjectRef, // use this to for the join
ReportDate = p.CreatedDate,
emotion = p.joy > p.fear ? p.joy > p.sadness ? p.joy > p.disgust ? p.joy > p.anger ? "Joy" : "Joy" : "Joy" :
p.fear > p.sadness ? p.fear > p.disgust ? p.fear > p.anger ? "Fear" : "Fear" : "Fear" : "Fear" :
p.sadness > p.disgust ? p.sadness > p.anger ? "Sadness" : "Sadness" :
p.disgust > p.anger ? "Disgust" :
"Anger",
score = p.joy > p.fear ? p.joy > p.sadness ? p.joy > p.disgust ? p.joy > p.anger ? p.joy : p.joy : p.joy :
p.fear > p.sadness ? p.fear > p.disgust ? p.fear > p.anger ? p.fear : p.fear : p.fear : p.fear :
p.sadness > p.disgust ? p.sadness > p.anger ? p.sadness : p.sadness :
p.disgust > p.anger ? p.disgust :
p.anger,
}).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
将所有内容放入键值元组列表中,对其进行排序,然后按值选择最高值:
var nameMax = (new[] {
Tuple.Create("joy", p.joy),
Tuple.Create("fear", p.fear),
Tuple.Create("sadness", p.sadness),
Tuple.Create("disgust", p.disgust),
Tuple.Create("anger", p.anger)
}).OrderByDescending(t => t.Item2).First().Item1;
Run Code Online (Sandbox Code Playgroud)