Home > Software design >  More than one column back in linq when using Take 1
More than one column back in linq when using Take 1

Time:10-31

So I have a table that has stats for workouts in that table there two fields Feet and Inches. I work out the longest jump by going to linq like this.

var playerBroadJumpHistory = await api.GetAllWorkOutsByPlayerId(playerId);
var maxBroadJumpFeet = (from u in playerBroadJumpHistory
                        where u.PlayerId == playerId
                        orderby u.BroadJumpFeet descending
                        select u).Take(1);

But I also want to get the value of inches which is u.BroadJumpInches how would I do that in the above example but retain both elements?. I am obviously doing this to get the max distance the person jumped over there scores but I need both columns of data so can display them.

Could I just create a new object using the select ?

CodePudding user response:

I figured it out what I nedded to do was this.

var maxBroadJumpFeet = (from u in playerBroadJumpHistory
                        where u.PlayerId == playerId
                        orderby u.BroadJumpFeet descending
                        select new {Feet = u.BroadJumpFeet, 
                       Inches =u.BroadJumpInches}).Take(1);

Simply declaring a new object was enough.

But what if I wanted both columns to be included in the max for example if it was 5FT6 and 5FT3 obv I want it return 5FT6

CodePudding user response:

It should be multiple columns that is u.BroadJumpFeet, u.BroadJumpInches

var maxBroadJumpFeet = (from u in playerBroadJumpHistory
                        where u.PlayerId == playerId
                        orderby u.BroadJumpFeet, u.BroadJumpInches  descending
                        select new {Feet = u.BroadJumpFeet, 
                       Inches =u.BroadJumpInches}).Take(1);
  • Related