1

For the following scenario:

Id  Total points    Correct Trends
1       12                8
2       12                4
3       10                5
4       10                7
5       10                2

Lets say I am the person with Id = 3.

I want to calculate the Ids which have Total points and Correct Trends greater than me.

If I use the below query then I will not get the user with Id=2 as his Correct Trends is less then mine.

var allAbove = (from item in userQuery
               where (item.Id!= myData.Id && 
               item.TotalPoints >= (myData.TotalPoints) &&
               item.CorrectTrends >= (myData.CorrectTrends) ) 

I am not able to find a way to check that when Total Points are equal then look for Correct Trends other wise check for Total Points only

0

3 Answers 3

2

The logic is actually straight forward:

var allAbove = (from item in userQuery
           where (item.Id!= myData.Id && 
           item.TotalPoints > myData.TotalPoints ||
           (item.TotalPoints == myData.TotalPoints && 
           item.CorrectTrends >= myData.CorrectTrends)));

It checks if either total points is greater or total points are equal and correct trends are greater.

Sign up to request clarification or add additional context in comments.

1 Comment

It was too easy but my mind is locked at the moment. Thanks a lot :)
1

Since you want to use CorrectTrends only for tie breaking, the correct expression should look like this:

item.Id!= myData.Id && (
       item.TotalPoints > myData.TotalPoints      // "Wins" on total points
||    (item.TotalPoints == myData.TotalPoints     // Same total points,
    && item.CorrectTrends > myData.CorrectTrends) // but higher correct trends
)

Note that >= need to be replaced with > to avoid items with ties.

Comments

0

Since i like method chain more:

var you = items.Where(x=>x.Id == myId).FirstOrDefault();
var allAbove = items.Where(x=>x.Id!=you.Id)
.Where(x=>x.TotalPoint>you.TotalPoints || x.CorrectTrends>you.CorrectTrends);

I'll let you do the null checking :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.