CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
LinqObservableCollection.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="LinqObservableCollection.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Synchronized view over a source list, </summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections;
10using System.Collections.Generic;
11using System.Collections.Specialized;
12using System.Linq;
13using System.Linq.Expressions;
14
15namespace Csla
16{
23 public class LinqObservableCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>,
24 IList, ICollection, INotifyCollectionChanged
25 {
29 public event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
30
31 private System.Collections.ObjectModel.ObservableCollection<T> _baseCollection;
32 private List<T> _filteredCollection;
33 private bool _suppressEvents = false;
40 public LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection<T> source, IEnumerable<T> queryResult)
41 : this(source, queryResult.ToList())
42 { }
43
50 public LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection<T> source, List<T> queryResult)
51 {
52 _filteredCollection = queryResult;
53 _baseCollection = source;
54 _baseCollection.CollectionChanged += (o, e) =>
55 {
56 if (!_suppressEvents)
57 {
58 NotifyCollectionChangedEventArgs newE = null;
59 T item;
60 int index;
61 switch (e.Action)
62 {
63 case NotifyCollectionChangedAction.Add:
64 item = (T)e.NewItems[0];
65 index = e.NewStartingIndex;
66 if (index > _filteredCollection.Count)
67 index = _filteredCollection.Count;
68 _filteredCollection.Insert(index, item);
69 newE = new NotifyCollectionChangedEventArgs(
70 e.Action, item, _filteredCollection.IndexOf(item));
71 break;
72 case NotifyCollectionChangedAction.Remove:
73 item = (T)e.OldItems[0];
74 index = _filteredCollection.IndexOf(item);
75 if (index > -1)
76 {
77 _filteredCollection.Remove(item);
78 newE = new NotifyCollectionChangedEventArgs(e.Action, item, index);
79 }
80 break;
81 case NotifyCollectionChangedAction.Replace:
82 index = _filteredCollection.IndexOf((T)e.OldItems[0]);
83 if (index > -1)
84 {
85 _filteredCollection[index] = (T)e.NewItems[0];
86 newE = new NotifyCollectionChangedEventArgs(
87 e.Action,
88 e.NewItems, e.OldItems, index);
89 }
90 break;
91 case NotifyCollectionChangedAction.Reset:
92 if (source.Count == 0)
93 {
94 _filteredCollection.Clear();
95 newE = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
96 }
97 break;
98 default:
99 break;
100 }
101 if (newE != null)
103 }
104 };
105 }
106
111 protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
112 {
113 if (!_suppressEvents && CollectionChanged != null)
114 CollectionChanged(this, e);
115 }
116
122 public IList<T> Source
123 {
124 get { return _baseCollection; }
125 }
126
131 public List<T> QueryResult
132 {
133 get { return _filteredCollection; }
134 }
135
140 public int IndexOf(T item)
141 {
142 return _filteredCollection.IndexOf(item);
143 }
144
150 public void Insert(int index, T item)
151 {
152 _baseCollection.Insert(index, item);
153 }
154
159 public void RemoveAt(int index)
160 {
161 _baseCollection.Remove(_filteredCollection[index]);
162 }
163
169 public T this[int index]
170 {
171 get
172 {
173 return _filteredCollection[index];
174 }
175 set
176 {
177 var idx = _baseCollection.IndexOf(_filteredCollection[index]);
178 _baseCollection[idx] = value;
179 }
180 }
181
186 public void Add(T item)
187 {
188 _baseCollection.Add(item);
189 }
190
199 public void Clear()
200 {
201 _suppressEvents = true;
202 foreach (var item in _filteredCollection)
203 _baseCollection.Remove(item);
204 _suppressEvents = false;
205 _filteredCollection.Clear();
207 new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
208 }
209
215 public bool Contains(T item)
216 {
217 return _filteredCollection.Contains(item);
218 }
219
225 public void CopyTo(T[] array, int arrayIndex)
226 {
227 _filteredCollection.CopyTo(array, arrayIndex);
228 }
229
233 public int Count
234 {
235 get { return _filteredCollection.Count; }
236 }
237
242 public bool IsReadOnly
243 {
244 get
245 {
246 var obj = _baseCollection as ICollection<T>;
247 if (obj != null)
248 return obj.IsReadOnly;
249 else
250 return false;
251 }
252 }
253
258 public bool Remove(T item)
259 {
260 return _baseCollection.Remove(item);
261 }
262
266 public IEnumerator<T> GetEnumerator()
267 {
268 return _filteredCollection.GetEnumerator();
269 }
270
274 IEnumerator IEnumerable.GetEnumerator()
275 {
276 return _filteredCollection.GetEnumerator();
277 }
278
283 public int Add(object value)
284 {
285 return ((IList)_baseCollection).Add(value);
286 }
287
293 public bool Contains(object value)
294 {
295 return _filteredCollection.Contains((T)value);
296 }
297
302 public int IndexOf(object value)
303 {
304 return _filteredCollection.IndexOf((T)value);
305 }
306
312 public void Insert(int index, object value)
313 {
314 _baseCollection.Insert(index, (T)value);
315 }
316
321 public bool IsFixedSize
322 {
323 get { return ((IList)_baseCollection).IsFixedSize; }
324 }
325
330 public void Remove(object value)
331 {
332 _baseCollection.Remove((T)value);
333 }
334
335 object IList.this[int index]
336 {
337 get
338 {
339 return this[index];
340 }
341 set
342 {
343 this[index] = (T)value;
344 }
345 }
346
352 public void CopyTo(Array array, int index)
353 {
354 ((IList)_filteredCollection).CopyTo(array, index);
355 }
356
361 public bool IsSynchronized
362 {
363 get { return ((IList)_baseCollection).IsSynchronized; }
364 }
365
369 public object SyncRoot
370 {
371 get { return ((IList)_baseCollection).SyncRoot; }
372 }
373 }
374
378 public static class LinqObservableCollectionExtension
379 {
384 public static LinqObservableCollection<C> ToSyncList<C>(this IEnumerable<C> queryResult, System.Collections.ObjectModel.ObservableCollection<C> source)
385 {
386 return new LinqObservableCollection<C>(source, queryResult);
387 }
388
393 public static LinqObservableCollection<C> ToSyncList<C>(this System.Collections.ObjectModel.ObservableCollection<C> source, IEnumerable<C> queryResult)
394 {
395 return new LinqObservableCollection<C>(source, queryResult);
396 }
397
402 public static LinqObservableCollection<C> ToSyncList<C>(this System.Collections.ObjectModel.ObservableCollection<C> source, Expression<Func<C, bool>> expr)
403 {
404 IEnumerable<C> sourceEnum = source.AsEnumerable<C>();
405 var output = sourceEnum.Where<C>(expr.Compile());
406 return new LinqObservableCollection<C>(source, output.ToList());
407 }
408 }
409}
Synchronized view over a source list, filtered, sorted and ordered based on a query result.
System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged
Event raised when the underlying source list is changed.
IList< T > Source
Gets the source list (usually a BusinessListBase) object that contains/manages all data items.
virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
Raises the CollectionChanged event.
bool IsFixedSize
Gets a value indicating whether the source list has a fixed size.
List< T > QueryResult
Gets the query result object used to filter the view.
void Add(T item)
Adds an item to the end of the list.
void CopyTo(T[] array, int arrayIndex)
Copies the contents of the list to an array.
void RemoveAt(int index)
Removes item at specified index.
void CopyTo(Array array, int index)
Copies the contents of the list to an array.
int Count
Gets the number of items in the list.
void Insert(int index, T item)
Inserts item into specified position in list.
bool IsReadOnly
Gets a value indicating whether the source list is read-only.
int IndexOf(object value)
Gets the positional index of the item.
bool Contains(T item)
Gets a value indicating whether the list contains the specified item.
int Add(object value)
Adds an item to the end of the list.
bool Contains(object value)
Gets a value indicating whether the list contains the specified item.
LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection< T > source, IEnumerable< T > queryResult)
bool Remove(T item)
Removes specified item from the list.
IEnumerator< T > GetEnumerator()
Gets an enumerator for the list.
object SyncRoot
Gets the SyncRoot from the source list.
int IndexOf(T item)
Gets the positional index of the item.
void Remove(object value)
Removes specified item from the list.
void Insert(int index, object value)
Inserts item into specified position in list.
bool IsSynchronized
Gets a value indicating whether the source list is synchronized.
LinqObservableCollection(System.Collections.ObjectModel.ObservableCollection< T > source, List< T > queryResult)
Creates a new instance of the observable view.