Problem:
I have a wpf DataGrid, which is bound to a list of objects from my custom model class. I need to fullfil following requirements:
- There is ony a set amount of models in this list (think of an online computer game with 8 players for example)
- Each Model has its own unique ID
- Models can be added to and removed from the list in runtime
- On initialization each model should get the lowest free ID from the list
- I don't want to iterate through the list each time a new model is added
- All of this needs to happen back-end, so I can't use any xaml code (which is also why I didn't set the wpf tag)
I am looking for something simular to this question, but I struggle to convert my list to something that would be compatible with the Enumerable.Except method.
Here is a little example:
Model:
public class MyModel
{
    public MyModel(int mID, string mName, string mScore)
    {
      ID = mID;
      Name = mName;
      Score = mScore;
    }
    public int ID;
    public string Name;
    public string Score;
  }
Trying to add a new model with the lowest free ID to my list
  List<MyModel> list = new List<MyModel>();
  list.Add(new MyModel(0, "Rodney", "189"));
  list.Add(new MyModel(1, "Janet", "169"));
  list.Add(new MyModel(2, "John", "110"));
  list.Add(new MyModel(3, "Samantha", "160"));
  list.Add(new MyModel(4, "Daniel", "156"));
  list.Add(new MyModel(5, "Jack", "89"));
  list.RemoveAll(x => x.ID == 1);
  var result = Enumerable.Range(list.Min(m => m.ID), list.Count).Except(list).First();
  list.Add(new MyModel(result, "Carolyn", "159")); //Should be 1
I probably have to use some kind of lambda expressions, as I had to for the list.Min() Method, but it already took me ages to get this right.
 
     
     
     
     
    