Reputation: 73
I Have 5 stored variables, and I want to find the variable name with the largest value.
I've used this answer as the basis for the below code. However I don't understand what it is doing and therefore cannot be sure I'm getting the correct result.
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"
I'm am hoping someone can validate, or adjust as necessary.
use case: I want to return the variable name with the highest value, where there is two or more variables with the same number I want to return them in this sequence. Anger, Fear, Sadness, Disgust, Joy
Here is the larger code segment which is 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();
Upvotes: 1
Views: 1955
Reputation: 225044
Put them into a foldable data structure in the correct order, and voilà:
var emotions = new[] {
Tuple.Create(p.anger, Emotion.Anger),
Tuple.Create(p.fear, Emotion.Fear),
Tuple.Create(p.sadness, Emotion.Sadness),
Tuple.Create(p.disgust, Emotion.Disgust),
Tuple.Create(p.joy, Emotion.Joy),
};
var highestEmotions = emotions
.MaxByAll(t => t.Item1)
.Select(e => e.Item2);
After defining an enum:
enum Emotion {
Anger,
Fear,
Sadness,
Disgust,
Joy,
}
and an extension to take a sequence and get the maximum values (preserving order) according to a certain key:
public static IList<T> MaxByAll<T, TKey>(this IEnumerable<T> items, Func<T, TKey> key) where TKey : IComparable {
var enumerator = items.GetEnumerator();
enumerator.MoveNext();
var maxes = new List<T> { enumerator.Current };
TKey maxKey = key(enumerator.Current);
while (enumerator.MoveNext()) {
T current = enumerator.Current;
TKey currentKey = key(current);
int relation = currentKey.CompareTo(maxKey);
if (relation > 0) {
maxes.Clear();
maxes.Add(current);
maxKey = currentKey;
continue;
}
if (relation == 0) {
maxes.Add(current);
}
}
return maxes;
}
Upvotes: 1
Reputation: 674
We can do this by creating a struct or a class -
struct p
{
public double joy, fear, sadness, disgust, anger;
}
initialize it -
var p = new p();
p.joy = .6;
p.fear = .5;
p.sadness = .4;
p.disgust = .1;
p.anger = .7;
Get variable/field with max value -
var field = p.GetType().GetFields().OrderByDescending(f=>f.GetValue(p)).First().Name;
Console.WriteLine(field);
Results anger
Upvotes: 0
Reputation: 726809
Put everything in a list of key-value tuples, sort it, and pick the highest one by value:
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;
Upvotes: 7