示例#1
0
 public void ForEach_ObservableCollection()
 {
     ObservableCollection<int> list = new ObservableCollection<int>(new int[] { 1, 2, 3, 4 }.ToList());
     int result = 0;
     list.ForEach<int>(i => result += i);
     Assert.That(result, Is.EqualTo(10));
 }
 // called by ObjectDataSource - set sort, grouping and filter info before calling load
 internal EntityQueryPagedCollectionView(EntityQuery query, int pageSize, int loadSize, 
   SortDescriptionCollection sortDescriptors, ObservableCollection<GroupDescription> groupDescriptors, IPredicateDescription filter) 
  : this(query, pageSize, loadSize, true, false) {
   sortDescriptors.ForEach(d => SortDescriptions.Add(d));
   groupDescriptors.ForEach(d=> GroupDescriptions.Add(d));
   SetQueryFilter(filter);
   Refresh();
 }
    private void LoadView() {
      // Create and start loading the PCV.

      // Neither EQPCV nor PCV will work with anonymous types.
      if (AnonymousFns.IsAnonymousType(Query.ElementType)) {
        throw new NotSupportedException("Anonymous types are not currently supported.  To work around this limitation, you can instead project into a custom type.");
      }

      if (IsEntityQuery) {
        EntityManager.GetEntityGroup(Query.ElementType).EntityChanged += ObjectDataSource_EntityChanged;
      }

      IsLoadingData = true;
      HasChanges = false;

      // Convert from our "descriptors" to the "descriptions" known by a PCV.
      var sortFields = new SortDescriptionCollection();
      SortDescriptors.ForEach(s => sortFields.Add(s.ToSortDescription()));
      var groupFields = new ObservableCollection<GroupDescription>();
      GroupDescriptors.ForEach(g => groupFields.Add(g.ToGroupDescription()));

      // We'll use an EQPCV or PCV depending on a few factors: 1) an EntityQuery (so we can issue skip/take, 2) paging
      bool useEqpcv = Query is EntityQuery && PageSize > 0;

      if (useEqpcv) {
        EntityQueryPagedCollectionView pcv = null;
        var query = Query as EntityQuery;
        var filter = GetQueryFilter();
        if (sortFields.Count > 0 || groupFields.Count > 0) {
          pcv = new EntityQueryPagedCollectionView(query, PageSize, LoadSize, sortFields, groupFields, filter);
        } else {
          pcv = new EntityQueryPagedCollectionView(query, PageSize, LoadSize, true, true);
          pcv.SetQueryFilter(filter);
          pcv.Refresh();
        }
        Data = pcv;
        DataView = pcv;

      } else {
        // Use the standard PagedCollectionView (when paging isn't wanted or not an EntityQuery)
        IEntityQuery query = Query;
        if (Query is EntityQuery) {
          query = GetFilteredQuery();
        }
        EntityManager.ExecuteQueryAsync(query, args => {
          PagedCollectionView pcv = new PagedCollectionView(args.Results);
          sortFields.ForEach(d => pcv.SortDescriptions.Add(d));
          groupFields.ForEach(d => pcv.GroupDescriptions.Add(d));
          pcv.PageSize = PageSize;
          Data = pcv;
          DataView = pcv;
        });
      }
    }