gpt4 book ai didi

c# - 维护序数集合的正确顺序

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:36:05 25 4
gpt4 key购买 nike

我有一个简单的域对象:

class FavoriteFood
{
public string Name;
public int Ordinal;
}

我想要一个维护正确序数的域对象的集合。例如,给定 4 种最喜欢的食物:

Name: Banana, Ordinal: 1
Name: Orange, Ordinal: 2
Name: Pear, Ordinal: 3
Name: Watermelon, Ordinal: 4

如果我将 Pear 的序数更改为 4,它应该将 Watermelon 的序数降低为 3。

如果我添加一种新的最喜欢的食物(草莓),序数为 3,它应该将梨移至 4,将西瓜移至 5。

如果我将 Pear 的序数更改为 2,它应该将 Orange 移至 3。

如果我将西瓜的序数更改为 1,香蕉将增加到 2,橙色将增加到 3,而梨将增加到 4。

完成此任务的最佳方法是什么?

更新:域对象的名称属性是动态的并且基于用户输入。该对象必须具有此 Ordinal 属性,因为用户可以更改他们最喜欢的食物的显示顺序。此序数值保存在数据库中,在填充结构时,我无法保证按序号顺序添加项目。

我遇到的问题是,当底层域对象发生更改时,没有一种好的方法来更新列表中的其余项目。例如:

var favoriteFoods = new List<FavoriteFood>();
var banana = new FavoriteFood { Name = "Banana", Ordinal = 1};
favoriteFoods.Add(banana);
favoriteFoods.Add(new FavoriteFood { Name = "Orange", Ordinal = 2 });
banana.Ordinal = 2;
// at this point both Banana and Orange have the same ordinal in the list. How can we make sure that Orange's ordinal gets updated too?

到目前为止,我已尝试执行以下有效操作:

class FavoriteFood : INotifyPropertyChanging
{
public string Name;
public int Ordinal
{
get { return this.ordinal; }
set
{
var oldValue = this.ordinal;
if (oldValue != value && this.PropertyChanging != null)
{
this.PropertyChanging(new FavoriteFoodChangingObject { NewOrdinal = value, OldOrdinal = oldValue }, new PropertyChangingEventArgs("Ordinal"));
}
this.ordinal = value;
}
}

internal struct FavoriteFoodChangingObject
{
internal int NewOrdinal;
internal int OldOrdinal;
}

// THIS IS A TEMPORARY WORKAROUND
internal int ordinal;

public event PropertyChangingEventHandler PropertyChanging;
}

public class FavoriteFoodCollection : IEnumerable<FavoriteFood>
{
private class FavoriteFoodOrdinalComparer : IComparer<FavoriteFood>
{
public int Compare(FavoriteFood x, FavoriteFood y)
{
return x.Ordinal.CompareTo(y.Ordinal);
}
}

private readonly SortedSet<FavoriteFood> underlyingList = new SortedSet<FavoriteFood>(new FavoriteFoodOrdinalComparer());

public IEnumerator<FavoriteFood> GetEnumerator()
{
return this.underlyingList.GetEnumerator();
}

public void AddRange(IEnumerable<FavoriteFood> items)
{
foreach (var i in items)
{
this.underlyingList.Add(i);
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

private void UpdateOrdinalsDueToRemoving(FavoriteFood item)
{

foreach (var i in this.underlyingList.Where(x => x.Ordinal > item.Ordinal))
{
i.ordinal--;
}
}

public void Remove(FavoriteFood item)
{
this.underlyingList.Remove(item);
this.UpdateOrdinalsDueToRemoving(item);
}

public void Add(FavoriteFood item)
{
this.UpdateOrdinalsDueToAdding(item);
this.underlyingList.Add(item);
item.PropertyChanging += this.item_PropertyChanging;
}

private void item_PropertyChanging(object sender, PropertyChangingEventArgs e)
{
if (e.PropertyName.Equals("Ordinal"))
{
var ordinalsChanging = (FavoriteFood.FavoriteFoodChangingObject)sender;
this.UpdateOrdinalsDueToEditing(ordinalsChanging.NewOrdinal, ordinalsChanging.OldOrdinal);
}
}

private void UpdateOrdinalsDueToEditing(int newOrdinal, int oldOrdinal)
{

if (newOrdinal > oldOrdinal)
{

foreach (var i in this.underlyingList.Where(x => x.Ordinal <= newOrdinal && x.Ordinal > oldOrdinal))
{
//i.Ordinal = i.Ordinal - 1;
i.ordinal--;
}

}
else if (newOrdinal < oldOrdinal)
{

foreach (var i in this.underlyingList.Where(x => x.Ordinal >= newOrdinal && x.Ordinal < oldOrdinal))
{
//i.Ordinal = i.Ordinal + 1;
i.ordinal++;
}
}
}

private void UpdateOrdinalsDueToAdding(FavoriteFood item)
{

foreach (var i in this.underlyingList.Where(x => x.Ordinal >= item.Ordinal))
{
i.ordinal++;
}
}
}

这工作正常,但使用内部序数字段是一种奇怪的解决方法。它是必需的,这样 PropertyChangingEvent 就不会被无限引发。

最佳答案

只需使用 List<string> :

List<string> foods = new List<string> { "Banana", "Orange", "Pear" };
int ordinalOfOrange = foods.IndexOf("Orange");

如果它必须改变您描述的方式,那么“存储”该序数不是一个好主意。

关于c# - 维护序数集合的正确顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6232081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com