Share via

how to get the min value of IEnumerable?

mc 7,106 Reputation points
2026-05-19T04:19:29.0633333+00:00

User's image User's image

what I want to do is to make the Y begin from 0 and I get the min value and then - min value but I get - value. which is not right.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Varsha Dundigalla(INFOSYS LIMITED) 5,025 Reputation points Microsoft External Staff
2026-05-19T08:54:09.85+00:00

Thank you for reaching out.

The approach you are using to get the minimum value and adjust the Y values is correct. The idea is to find the smallest Y value and subtract it from all items so that the new minimum becomes 0.

However, getting negative values usually means the minimum value is not being taken from the same data that is being updated, or the data is changing before applying the calculation.

To make this work correctly, calculate the minimum value once from the same collection and then use that value for all updates, like this:

var values1 = values.ToArray();
 
var minY = values1.Min(v => v.Y);
for (int i = 0; i < values1.Length; i++)
{
    values1[i].Y = values1[i].Y - minY;
}

After this, the smallest Y value will always be 0, and other values will be adjusted correctly.

If negative values still appear, it indicates the source data may be changing before or during this operation.

Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AI answer

    2026-05-19T04:19:48.01+00:00

    Enumerable.Min already returns the minimum value of the sequence; it does not negate it. The negative value comes from the code that subtracts the minimum from each element.

    To shift all Y values so that the smallest Y becomes 0 (and all others are moved up accordingly), compute the minimum once and subtract it from each Y:

    var values1 = values.ToArray();
    int minY = values1.Min(v => v.Y);   // minimum Y in the sequence
    
    for (int i = 0; i < values1.Length; i++)
    {
        values1[i].Y -= minY;          // shift so that minY becomes 0
        // other updates
    }
    

    After this loop, the new minimum of values1.Select(v => v.Y) will be 0, and all Y values will be non‑negative.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.