本文整理汇总了C#中TrackingCollection.SetComparer方法的典型用法代码示例。如果您正苦于以下问题:C# TrackingCollection.SetComparer方法的具体用法?C# TrackingCollection.SetComparer怎么用?C# TrackingCollection.SetComparer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TrackingCollection
的用法示例。
在下文中一共展示了TrackingCollection.SetComparer方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultipleSortingAndFiltering
public void MultipleSortingAndFiltering()
{
var expectedTotal = 20;
var rnd = new Random(214748364);
var titles1 = Enumerable.Range(1, expectedTotal).Select(x => ((char)('a' + x)).ToString()).ToList();
var dates1 = Enumerable.Range(1, expectedTotal).Select(x => Now + TimeSpan.FromMinutes(x)).ToList();
var idstack1 = new Stack<int>(Enumerable.Range(1, expectedTotal).OrderBy(rnd.Next));
var datestack1 = new Stack<DateTimeOffset>(dates1);
var titlestack1 = new Stack<string>(titles1.OrderBy(_ => rnd.Next()));
var titles2 = Enumerable.Range(1, expectedTotal).Select(x => ((char)('c' + x)).ToString()).ToList();
var dates2 = Enumerable.Range(1, expectedTotal).Select(x => Now + TimeSpan.FromMinutes(x)).ToList();
var idstack2 = new Stack<int>(Enumerable.Range(1, expectedTotal).OrderBy(rnd.Next));
var datestack2 = new Stack<DateTimeOffset>(new List<DateTimeOffset>() {
dates2[2], dates2[0], dates2[1], dates2[3], dates2[5],
dates2[9], dates2[15], dates2[6], dates2[7], dates2[8],
dates2[13], dates2[10], dates2[16], dates2[11], dates2[12],
dates2[14], dates2[17], dates2[18], dates2[19], dates2[4],
});
var titlestack2 = new Stack<string>(titles2.OrderBy(_ => rnd.Next()));
var list1 = Observable.Defer(() => Enumerable.Range(1, expectedTotal)
.OrderBy(rnd.Next)
.Select(x => new Thing(idstack1.Pop(), titlestack1.Pop(), datestack1.Pop()))
.ToObservable())
.Replay()
.RefCount();
var list2 = Observable.Defer(() => Enumerable.Range(1, expectedTotal)
.OrderBy(rnd.Next)
.Select(x => new Thing(idstack2.Pop(), titlestack2.Pop(), datestack2.Pop()))
.ToObservable())
.Replay()
.RefCount();
var col = new TrackingCollection<Thing>(
list1.Concat(list2),
OrderedComparer<Thing>.OrderByDescending(x => x.CreatedAt).Compare,
(item, idx, list) => idx < 5
)
{ ProcessingDelay = TimeSpan.Zero };
var count = 0;
var evt = new ManualResetEvent(false);
col.Subscribe(t =>
{
if (++count == expectedTotal * 2)
evt.Set();
}, () => { });
evt.WaitOne();
evt.Reset();
// it's initially sorted by date, so id list should not match
CollectionAssert.AreNotEqual(list1.Select(x => x.Number).ToEnumerable(), list2.Select(x => x.Number).ToEnumerable());
var sortlist = list1.ToEnumerable().ToArray();
Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderByDescending(x => x.CreatedAt).Compare));
CollectionAssert.AreEqual(sortlist.Take(5), col);
col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.Number).Compare);
sortlist = list1.ToEnumerable().ToArray();
Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.Number).Compare));
CollectionAssert.AreEqual(sortlist.Take(5), col);
col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare);
sortlist = list1.ToEnumerable().ToArray();
Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare));
CollectionAssert.AreEqual(sortlist.Take(5), col);
col.SetComparer(OrderedComparer<Thing>.OrderByDescending(x => x.Title).Compare);
sortlist = list1.ToEnumerable().ToArray();
Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderByDescending(x => x.Title).Compare));
CollectionAssert.AreEqual(sortlist.Take(5), col);
col.SetComparer(OrderedComparer<Thing>.OrderBy(x => x.Title).Compare);
sortlist = list1.ToEnumerable().ToArray();
Array.Sort(sortlist, new LambdaComparer<Thing>(OrderedComparer<Thing>.OrderBy(x => x.Title).Compare));
CollectionAssert.AreEqual(sortlist.Take(5), col);
col.Dispose();
}
示例2: ChangingComparers
public void ChangingComparers()
{
var source = new Subject<Thing>();
var col = new TrackingCollection<Thing>(source, OrderedComparer<Thing>.OrderBy(x => x.CreatedAt).Compare) { ProcessingDelay = TimeSpan.Zero };
var count = 0;
var evt = new ManualResetEvent(false);
var list1 = new List<Thing> {
GetThing(1, 1, 9),
GetThing(2, 2, 8),
GetThing(3, 3, 7),
GetThing(4, 4, 6),
GetThing(5, 5, 5),
GetThing(6, 6, 4),
GetThing(7, 7, 3),
GetThing(8, 8, 2),
GetThing(9, 9, 1),
};
col.Subscribe(t =>
{
if (++count == list1.Count)
evt.Set();
}, () => { });
foreach (var l in list1)
Add(source, l);
evt.WaitOne();
evt.Reset();
CollectionAssert.AreEqual(col, list1);
col.SetComparer(null);
CollectionAssert.AreEqual(col, list1.Reverse<Thing>().ToArray());
col.Dispose();
}
示例3: DisposingThrows
public void DisposingThrows()
{
var col = new TrackingCollection<Thing>(Observable.Empty<Thing>());
col.Dispose();
Assert.Throws<ObjectDisposedException>(() => col.SetFilter(null));
Assert.Throws<ObjectDisposedException>(() => col.SetComparer(null));
Assert.Throws<ObjectDisposedException>(() => col.Subscribe());
Assert.Throws<ObjectDisposedException>(() => col.AddItem(GetThing(1)));
Assert.Throws<ObjectDisposedException>(() => col.RemoveItem(GetThing(1)));
}
示例4: ChangingSortUpdatesCollection
public void ChangingSortUpdatesCollection()
{
var source = new Subject<Thing>();
var col = new TrackingCollection<Thing>(
source,
OrderedComparer<Thing>.OrderBy(x => x.UpdatedAt).Compare,
(item, position, list) => item.UpdatedAt < Now + TimeSpan.FromMinutes(10))
{ ProcessingDelay = TimeSpan.Zero };
var count = 0;
var evt = new ManualResetEvent(false);
var list1 = new List<Thing> {
GetThing(1, 1),
GetThing(2, 2),
GetThing(3, 3),
GetThing(4, 4),
GetThing(5, 5),
GetThing(6, 6),
GetThing(7, 7),
GetThing(8, 8),
GetThing(9, 9),
};
col.Subscribe(t =>
{
if (++count == list1.Count)
evt.Set();
}, () => { });
foreach (var l in list1)
Add(source, l);
evt.WaitOne();
evt.Reset();
CollectionAssert.AreEqual(col, list1);
col.SetComparer(OrderedComparer<Thing>.OrderByDescending(x => x.UpdatedAt).Compare);
CollectionAssert.AreEqual(col, list1.Reverse<Thing>().ToArray());
col.Dispose();
}