C# 分组、排序,然后合并到可观察的集合?

C# 分组、排序,然后合并到可观察的集合?,c#,sorting,observablecollection,C#,Sorting,Observablecollection,我有一个具有“DisplayOrder”属性的项目列表。它可以有NULL或int值。如果它有int值,则它具有优先级,并且应该在可观察集合的第一组中。第1组中的项目也按显示顺序排序 如果为空,则它属于第二组,按字母顺序排序 然后,将第一组和第二组合并为一个主要的Items集合Observable集合,我将其绑定到一个ListView 这是我目前的代码,尽管我担心是否有一个非常理想的方法来完成它 var MainItemCollection = new ObservableCollection&l

我有一个具有“DisplayOrder”属性的项目列表。它可以有NULL或int值。如果它有int值,则它具有优先级,并且应该在可观察集合的第一组中。第1组中的项目也按显示顺序排序

如果为空,则它属于第二组,按字母顺序排序

然后,将第一组和第二组合并为一个主要的Items集合Observable集合,我将其绑定到一个ListView

这是我目前的代码,尽管我担心是否有一个非常理想的方法来完成它

var MainItemCollection = new ObservableCollection<MainItemViewModel>();
var ItemsWithProperty = new ObservableCollection<MainItemViewModel>();
var ItemsWithNullProperty = new ObservableCollection<MainItemViewModel>();

foreach (var item in DataObject.MainItems)
{
    if (item.DisplayOrder == null)
        ItemsWithNullProperty.Add(new MainItemViewModel(item));
    else
        ItemsWithProperty.Add(new MainItemViewModel(item));
}

ItemsWithProperty = new ObservableCollection<MainItemViewModel>(ItemsWithProperty.OrderBy(c => c.DisplayOrder));
ItemsWithNullProperty = new ObservableCollection<MainItemViewModel>(ItemsWithNullProperty.OrderBy(c => c.Title));

//Add those with priorities first sorted by DisplayOrder 1,2,3,4
foreach (var c in ItemsWithProperty)
{
    MainItemCollection.Add(c);
}

//Add those without priority sorted Alphabetically
foreach (var c in ItemsWithNullProperty)
{
    MainItemCollection.Add(c);
}
var MainItemCollection=新的ObservableCollection();
var ItemsWithProperty=新的ObservableCollection();
var ItemsWithNullProperty=新的ObservableCollection();
foreach(DataObject.MainItems中的变量项)
{
if(item.DisplayOrder==null)
添加(新的MainItemViewModel(item));
其他的
添加(新的MainItemViewModel(item));
}
ItemsWithProperty=newobserveCollection(ItemsWithProperty.OrderBy(c=>c.DisplayOrder));
ItemsWithNullProperty=newobserveCollection(ItemsWithNullProperty.OrderBy(c=>c.Title));
//添加优先级先按显示顺序1,2,3,4排序的内容
foreach(ItemsWithProperty中的变量c)
{
MainItemCollection.Add(c);
}
//添加那些没有按字母顺序排序的优先级
foreach(ItemsWithNullProperty中的变量c)
{
MainItemCollection.Add(c);
}

谢谢大家!

在执行此类操作时,您不需要所有中间的
observateCollection
s-您可以使用适当的数据结构,如数组、列表、字典、哈希集等或Linq查询。在这种特殊情况下,整个过程可以简化为这样的情况

var MainItemCollection = new ObservableCollection<MainItemViewModel>(DataObject.MainItems
    .OrderBy(item => item.DisplayOrder ?? int.MaxValue)
    .ThenBy(item => item.DisplayOrder == null ? item.Title : string.Empty)
);
var MainItemCollection=newobservetecollection(DataObject.MainItems
.OrderBy(item=>item.DisplayOrder??int.MaxValue)
.ThenBy(item=>item.DisplayOrder==null?item.Title:string.Empty)
);

在执行此类操作时,您不需要所有中间的
可观察收集
s-您可以使用适当的数据结构,如数组、列表、字典、哈希集等或Linq查询。在这种特殊情况下,整个过程可以简化为这样的情况

var MainItemCollection = new ObservableCollection<MainItemViewModel>(DataObject.MainItems
    .OrderBy(item => item.DisplayOrder ?? int.MaxValue)
    .ThenBy(item => item.DisplayOrder == null ? item.Title : string.Empty)
);
var MainItemCollection=newobservetecollection(DataObject.MainItems
.OrderBy(item=>item.DisplayOrder??int.MaxValue)
.ThenBy(item=>item.DisplayOrder==null?item.Title:string.Empty)
);

在执行此类操作时,您不需要所有中间的
可观察收集
s-您可以使用适当的数据结构,如数组、列表、字典、哈希集等或Linq查询。在这种特殊情况下,整个过程可以简化为这样的情况

var MainItemCollection = new ObservableCollection<MainItemViewModel>(DataObject.MainItems
    .OrderBy(item => item.DisplayOrder ?? int.MaxValue)
    .ThenBy(item => item.DisplayOrder == null ? item.Title : string.Empty)
);
var MainItemCollection=newobservetecollection(DataObject.MainItems
.OrderBy(item=>item.DisplayOrder??int.MaxValue)
.ThenBy(item=>item.DisplayOrder==null?item.Title:string.Empty)
);

在执行此类操作时,您不需要所有中间的
可观察收集
s-您可以使用适当的数据结构,如数组、列表、字典、哈希集等或Linq查询。在这种特殊情况下,整个过程可以简化为这样的情况

var MainItemCollection = new ObservableCollection<MainItemViewModel>(DataObject.MainItems
    .OrderBy(item => item.DisplayOrder ?? int.MaxValue)
    .ThenBy(item => item.DisplayOrder == null ? item.Title : string.Empty)
);
var MainItemCollection=newobservetecollection(DataObject.MainItems
.OrderBy(item=>item.DisplayOrder??int.MaxValue)
.ThenBy(item=>item.DisplayOrder==null?item.Title:string.Empty)
);

获取DisplayOrder=null的项目并按标题排序:

ItemsWithNullProperty=DataObject.MainItems.Where(x=>x.DisplayOrder==null).OrderBy(o=>o.Title).ToList();

使用DisplayOrder获取项目(除上述查询外的所有项目)&按DisplayOrder排序:

ItemsWithProperty= DataObject.MainItems.Except(ItemsWithNullProperty).OrderBy(o=>o.DisplayOrder).ToList();
在MainCollection中填写数据:

var allItems = MainItemCollection.Concat(ItemsWithProperty)
                                    .Concat(ItemsWithNullProperty)
                                    .ToList();

获取DisplayOrder=null的项目并按标题排序:

ItemsWithNullProperty=DataObject.MainItems.Where(x=>x.DisplayOrder==null).OrderBy(o=>o.Title).ToList();

使用DisplayOrder获取项目(除上述查询外的所有项目)&按DisplayOrder排序:

ItemsWithProperty= DataObject.MainItems.Except(ItemsWithNullProperty).OrderBy(o=>o.DisplayOrder).ToList();
在MainCollection中填写数据:

var allItems = MainItemCollection.Concat(ItemsWithProperty)
                                    .Concat(ItemsWithNullProperty)
                                    .ToList();

获取DisplayOrder=null的项目并按标题排序:

ItemsWithNullProperty=DataObject.MainItems.Where(x=>x.DisplayOrder==null).OrderBy(o=>o.Title).ToList();

使用DisplayOrder获取项目(除上述查询外的所有项目)&按DisplayOrder排序:

ItemsWithProperty= DataObject.MainItems.Except(ItemsWithNullProperty).OrderBy(o=>o.DisplayOrder).ToList();
在MainCollection中填写数据:

var allItems = MainItemCollection.Concat(ItemsWithProperty)
                                    .Concat(ItemsWithNullProperty)
                                    .ToList();

获取DisplayOrder=null的项目并按标题排序:

ItemsWithNullProperty=DataObject.MainItems.Where(x=>x.DisplayOrder==null).OrderBy(o=>o.Title).ToList();

使用DisplayOrder获取项目(除上述查询外的所有项目)&按DisplayOrder排序:

ItemsWithProperty= DataObject.MainItems.Except(ItemsWithNullProperty).OrderBy(o=>o.DisplayOrder).ToList();
在MainCollection中填写数据:

var allItems = MainItemCollection.Concat(ItemsWithProperty)
                                    .Concat(ItemsWithNullProperty)
                                    .ToList();

我觉得这是一种相当普遍的情况。 有一个已排序的ObservableCollection绑定到某个XAML UI,一旦有更多数据可用,UI就需要在不进行完全刷新的情况下进行更新。 每当像上面的建议一样创建新的ObservableCollection时,所有项目都将反弹,因此UI将完全更新

我很惊讶没有库方法来实现这一点。这是我想出的解决办法。希望有人会觉得有用

public static class ObservableCollectionExtensions
{
    public static void MergeSortedListIntoSortedObservableCollection<T>(this ObservableCollection<T> destination, IList<T> list, Func<T, T, int> compareFunc)
    {
        int dest_index = 0;
        int list_index = 0;

        while (list_index < list.Count)
        {
            if (dest_index >= destination.Count)
            {
                destination.Add(list[list_index]);
                list_index++;
                dest_index++;
            }
            else
            {
                if (compareFunc(destination[dest_index], list[list_index]) > 0)
                {
                    destination.Insert(dest_index, list[list_index]);
                    list_index++;
                    dest_index++;
                }
                else
                {
                    dest_index++;
                }
            }
        }
    }
}
公共静态类ObservableCollectionExtensions
{
public static void MergeSortedListIntoSortedObservableCollection(此ObservableCollection目标、IList列表、Func compareFunc)
{
int dest_索引=0;
int list_index=0;
while(列表索引<列表计数)
{
if(dest_index>=destination.Count)
{
destination.Add(列表[列表索引]);
list_index++;
dest_index++;
}
其他的
{
if(compareFunc(目的地[目的地索引],列表[列表索引])大于0)
{
destination.Insert(dest_index,list[list_index]);
list_index++;
dest_index++;
}
其他的
{
dest_index++;
}
}
}
}
}

我觉得这是一种非常常见的情况。 有一个已排序的ObservableCollection绑定到某个XAML UI,一旦有更多数据可用,UI就需要在不进行完全刷新的情况下进行更新。 每当新的可观测集合