Optimizing location updates when adding a new object I'm trying to keep a bunch of Objects called Machine in their proper order. I was wondering if it'd be possible to further optimize the function HandleLocationCount for performance.
Any general feedback is of course also welcome. The function HandleLocationCount is only executed when a new Machine object is added.
The Machine object is below, it references a Section object, which acts as a container for the various machines.
public class Machine
{
    [Key]
    public int ID { get; set; }

    public string OrderNo { get; set; }

    public Section Section { get; set; }

    public int LocationInSection { get; set; }

    public ForcedLocation ForcedLocation { get; set; } = ForcedLocation.Blank;
}

Section object &amp; enum for completeness; it's an enum instead of a boolean for future proofing:
public class Section
{
    [Key]
    public int ID { get; set; }

    public string Name { get; set; }

    public string OrderNo { get; set; }

    public IEnumerable&lt;Machine&gt; MachinesInSection { get; set; }
}

public enum ForcedLocation
{
    /// &lt;summary&gt;
    /// Does nothing for the location.
    /// &lt;/summary&gt;
    Blank,

    /// &lt;summary&gt;
    /// This entity is always last in the location.
    /// &lt;/summary&gt;
    Keep_Last,
}

The function that calls the HandleLocationCount function looks like this; a user is able to manually set the location if needed.
    public Machine Add(Machine entity)
    {
        if (entity.LocationInSection == 0)
        {
            IEnumerable&lt;Machine&gt; AmountMachines = GetMachinesByOrderNoAndSection(entity.OrderNo, entity.Section);
            entity.LocationInSection = handleLocationCount(AmountMachines);
        }

        _machineRepository.Add(entity);

        return entity;
    }

The function that returns the index just searches every machine for their order number &amp; name, the repository itself is just an interface that has a generic index function
public IEnumerable&lt;Machine&gt; GetMachinesByOrderNoAndSection(string orderno, Section section)
    {
        return _machineRepository.Index()
         .Where(m =&gt; m.OrderNo == orderno &amp;&amp; m.Section == section);
    }

And the code for the location looks like this, it updates older machines if this is needed; else just passes the new location for the machine
    private int HandleLocationCount(IEnumerable&lt;Machine&gt; AmountOfMachines)
    {
        int newLocation = AmountOfMachines.Count() + 1;

        foreach (var Machine in AmountOfMachines)
        {
            int oldLocation = Machine.LocationInSection;
            switch (Machine.ForcedLocation)
            {
                case ForcedLocation.Blank:
                    break;
                case ForcedLocation.Keep_Last:
                    Machine.LocationInSection = AmountOfMachines.Count() + 1;
                    break;
                default:
                    break;
            }

            if (Machine.LocationInSection != oldLocation)
            {
                newLocation = oldLocation;
                Update(Machine);
            }
        }

        return newLocation;
    }
The method GetMachinesByOrderNoAndSection returns an IEnumerable&lt;Machine&gt;. Depending on how the method is implemented, iterating the enumeration may query the database. In HandleLocationCount you are iterating it and also calling .Count() on it several times. The LINQ Count extension method might also iterate the enumeration.
Make sure to iterate it only once, e.g. by working on a list crated with var list = AmountOfMachines.ToList();, if something like this is not already done in GetMachinesByOrderNoAndSection.
Also, you could use two distinct variables for storing newLocation and AmountOfMachines.Count() + 1 and call the latter only once.

I am not sure to understand the logic right. But you seem to assign the same AmountOfMachines.Count() + 1 to all the Keep_Last machines. If this is the case, you could simply assign them a constant number like Int32.MaxValue. They would be kept last once and for all.
Another possible approach is to partition the number range into two ranges:

[1 .. Int32.MaxValue / 2] for Blank.
[Int32.MaxValue / 2 + 1 .. Int32.MaxValue] for Keep_Last.

Adavantages:

The machines stay in their respective group.
They are ordered.
Adding a machine at the end of a group does not require the LocationInSection of others to be updated.

Instead of getting the count, you would get the maximium value in the two groups.
int nextBlank = machineList
    .Where(m =&gt; m.ForcedLocation == ForcedLocation.Blank)
    .DefaultIfEmpty(0)
    .Max() + 1;
int nextKeepLast = machineList
    .Where(m =&gt; m.ForcedLocation == ForcedLocation.Keep_Last)
    .DefaultIfEmpty(Int32.MaxValue / 2)
    .Max() + 1;