linq expressions is to complex for me when i need to do complex searches over list.
let's say i have List
public class Object {
var length;
var height;
var depth;
}
How to filter objects id by max length, if there are more than one index, then filter by max width, if there are more than one index, then filter by max depth, if there more then one index return first one.
i found max values:
float maxWidth = boxes.Max(x => x.width);
float maxHeight = boxes.Max(x => x.height);
float maxDepth = boxes.Max(x => x.depth);
then i used for loop to get values. And it's ok, but after each loop i have to create another list to get another value and etc.
private GameObject largestSurfaceBox()
{
//find max length.width, height box
float maxWidth = boxes.Max(x => x.GetComponent<Box>().dimensions.x);
List<GameObject> maxWidthBoxes = new List<GameObject>();
for (int i = 0; i < boxes.Count; i++) {
if (System.Math.Abs(boxes[i].GetComponent<Box>().dimensions.x - maxWidth) < float.Epsilon) {
maxWidthBoxes.Add(boxes[i]);
}
}
float maxHeight = maxWidthBoxes.Max(x => x.GetComponent<Box>().dimensions.y);
List<GameObject> maxHeightBoxes = new List<GameObject>();
for (int i = 0; i < maxWidthBoxes.Count; i++) {
if (System.Math.Abs(maxWidthBoxes[i].GetComponent<Box>().dimensions.y - maxHeight) < float.Epsilon) {
maxHeightBoxes.Add(boxes[i]);
}
}
List<GameObject> maxDepthBoxes = new List<GameObject>();
float maxDepth = maxHeightBoxes.Max(x => x.GetComponent<Box>().dimensions.z);
for (int i = 0; i < maxHeightBoxes.Count; i++) {
if (System.Math.Abs(maxHeightBoxes[i].GetComponent<Box>().dimensions.z - maxDepth) < float.Epsilon) {
maxDepthBoxes.Add(boxes[i]);
}
}
return maxDepthBoxes[0];
}
How to do that elegant linq way with if statements if possible?