Browse Source

Grouping on original list using LINQ

reports
Peter Forstmeier 12 years ago
parent
commit
39d90e2d6f
  1. 1
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj
  2. 197
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/CollectionDataSource.cs
  3. 1
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj
  4. 32
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/CollectionHandlingFixture.cs
  5. 88
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/DataSourceGroupingFixture.cs

1
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj

@ -88,6 +88,7 @@ @@ -88,6 +88,7 @@
<Compile Include="Src\Collections.cs" />
<Compile Include="Src\Configuration\AssemblyInfo.cs" />
<Compile Include="Src\DataManager\Listhandling\CollectionSource.cs" />
<Compile Include="Src\DataManager\Listhandling\CollectionDataSource.cs" />
<Compile Include="Src\DataManager\Listhandling\IndexList.cs" />
<Compile Include="Src\DataSource\Comparer\BaseComparer.cs" />
<Compile Include="Src\DataSource\Comparer\GroupComparer.cs" />

197
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/CollectionDataSource.cs

@ -0,0 +1,197 @@ @@ -0,0 +1,197 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using ICSharpCode.Reporting.BaseClasses;
using ICSharpCode.Reporting.DataSource;
using ICSharpCode.Reporting.DataSource.Comparer;
using ICSharpCode.Reporting.Interfaces;
using ICSharpCode.Reporting.Interfaces.Data;
using ICSharpCode.Reporting.Items;
namespace ICSharpCode.Reporting.DataManager.Listhandling
{
/// <summary>
/// Description of DataSource.
/// </summary>
public class CollectionDataSource:IDataViewHandling
{
readonly DataCollection<object> baseList;
readonly ReportSettings reportSettings;
readonly Type elementType;
readonly PropertyDescriptorCollection listProperties;
public CollectionDataSource(IEnumerable list, Type elementType, ReportSettings reportSettings)
{
this.elementType = elementType;
this.reportSettings = reportSettings;
baseList = CreateBaseList(list, elementType);
this.listProperties = this.baseList.GetItemProperties(null);
}
public IndexList IndexList {
get {
throw new NotImplementedException();
}
}
public System.Collections.ObjectModel.Collection<ICSharpCode.Reporting.BaseClasses.AbstractColumn> AvailableFields {
get {
var availableFields = new Collection<AbstractColumn>();
foreach (PropertyDescriptor p in this.listProperties){
availableFields.Add (new AbstractColumn(p.Name,p.PropertyType));
}
return availableFields;
}
}
public int Count {
get {
return baseList.Count;
}
}
public int CurrentPosition {
get {
throw new NotImplementedException();
}
set {
throw new NotImplementedException();
}
}
public object Current {get; private set;}
public void Sort()
{
throw new NotImplementedException();
}
#region Grouping
public void Group()
{
groupedList = GroupInternal();
groupEnumerator = groupedList.GetEnumerator();
groupEnumerator.MoveNext();
listEnumerator = groupEnumerator.Current.GetEnumerator();
listEnumerator.MoveNext();
Current = listEnumerator.Current;
}
IEnumerable<IGrouping<object, object>> GroupInternal () {
var properties = listProperties.Find(reportSettings.GroupColumnCollection[0].ColumnName,true);
var property = listProperties.Find("Randomint",true);
var groupedList = baseList.OrderBy(o => o.GetType().GetProperty(property.Name).GetValue(o, null) )
.GroupBy(a => a.GetType().GetProperty(properties.Name).GetValue(a, null)).OrderBy(c => c.Key);
return groupedList;
}
#endregion
public void Bind()
{
if (reportSettings.GroupColumnCollection.Any()) {
this.Group();
} else {
this.Sort ();
}
}
#region Fill
private IEnumerable<IGrouping<object, object>> groupedList;
private IEnumerator<IGrouping<object, object>> groupEnumerator;
private IEnumerator<object> listEnumerator;
public void Fill(System.Collections.Generic.List<ICSharpCode.Reporting.Interfaces.IPrintableObject> collection)
{
foreach (IPrintableObject item in collection)
{
var dbItem = item as IDataItem;
if (dbItem != null) {
dbItem.DBValue = String.Empty;
dbItem.DBValue = ReadValueFromProperty(dbItem.ColumnName);
if (String.IsNullOrEmpty(dbItem.DataType)) {
dbItem.DataType = SetTypeFromProperty(dbItem.ColumnName).ToString();
}
}
}
}
string ReadValueFromProperty (string columnName) {
var p = listProperties.Find(columnName,true);
return p.GetValue(Current).ToString();
}
Type SetTypeFromProperty (string columnName) {
var p = listProperties.Find(columnName,true);
return p.PropertyType;
}
public bool MoveNext()
{
var canMove = listEnumerator.MoveNext();
if (! canMove) {
var groupCanMove = groupEnumerator.MoveNext();
if (groupCanMove) {
listEnumerator = groupEnumerator.Current.GetEnumerator();
canMove = listEnumerator.MoveNext();
Current = listEnumerator.Current;
} else {
Console.WriteLine("end");
}
} else {
Current = listEnumerator.Current;
}
return canMove;
}
#endregion
public void Reset()
{
throw new NotImplementedException();
}
DataCollection<object> CreateBaseList(IEnumerable source, Type elementType)
{
var list = new DataCollection<object>(elementType);
list.AddRange(source);
return list;
}
PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors){
if (listAccessors != null && listAccessors.Length > 0){
var t = this.elementType;
t = listAccessors.Aggregate(t,
(current, pd) => (Type) PropertyTypeHash.Instance[current, pd.Name]);
// if t is null an empty list will be generated
return ExtendedTypeDescriptor.GetProperties(t);
}
return ExtendedTypeDescriptor.GetProperties(elementType);
}
}
}

1
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj

@ -66,6 +66,7 @@ @@ -66,6 +66,7 @@
<ItemGroup>
<Compile Include="src\DataSource\ContributorsList.cs" />
<Compile Include="src\DataSource\CollectionHandlingFixture.cs" />
<Compile Include="src\DataSource\DataSourceGroupingFixture.cs" />
<Compile Include="src\Expressions\Aggregates\AggregateFuctionHelper.cs" />
<Compile Include="src\Expressions\Aggregates\SumAggregate.cs" />
<Compile Include="src\Expressions\IntegrationTests\FieldsFixture.cs" />

32
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/CollectionHandlingFixture.cs

@ -105,7 +105,7 @@ namespace ICSharpCode.Reporting.Test.DataSource @@ -105,7 +105,7 @@ namespace ICSharpCode.Reporting.Test.DataSource
#region Grouping
/*
[Test]
public void GroupbyOneColumn () {
var rs = new ReportSettings();
@ -130,37 +130,33 @@ namespace ICSharpCode.Reporting.Test.DataSource @@ -130,37 +130,33 @@ namespace ICSharpCode.Reporting.Test.DataSource
new BaseDataItem(){
ColumnName = "GroupItem"
},
new BaseDataItem(){
ColumnName = "RandomInt"
}
};
var rs = new ReportSettings();
rs.GroupColumnCollection.Add( new GroupColumn("GroupItem",1,ListSortDirection.Ascending));
rs.GroupColumnCollection.Add( new GroupColumn("RandomInt",1,ListSortDirection.Ascending));
var collectionSource = new CollectionSource (list,typeof(Contributor),rs);
collectionSource.Bind();
int i = 0;
do {
collectionSource.Fill_Test(ric);
Console.WriteLine("first : <{0}> Last <{1}> Group <{2}>",((BaseDataItem)ric[0]).DBValue,
collectionSource.Fill(ric);
Console.WriteLine("first : <{0}> Last <{1}> Group <{2}> Randomint <{3}>",((BaseDataItem)ric[0]).DBValue,
((BaseDataItem)ric[1]).DBValue,
((BaseDataItem)ric[2]).DBValue);
((BaseDataItem)ric[2]).DBValue,
((BaseDataItem)ric[3]).DBValue);
i ++;
}while (collectionSource.MoveNext_Test_List());
}while (collectionSource.MoveNext());
Assert.That(i,Is.EqualTo(collectionSource.Count));
}
/*
[Test]
public void bla () {
var s = list.OrderBy(a => a.Lastname);
var x = s.GroupBy(y => y.GroupItem);
foreach (var group in x) {
Console.WriteLine("{0} - {1}",group.Key,group.GetType().ToString());
foreach (var element in group) {
Console.WriteLine(element.Firstname);
}
}
}
*/
#endregion
#region Sort

88
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/DataSourceGroupingFixture.cs

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.ComponentModel;
using ICSharpCode.Reporting.BaseClasses;
using ICSharpCode.Reporting.DataManager.Listhandling;
using ICSharpCode.Reporting.Interfaces;
using ICSharpCode.Reporting.Items;
using NUnit.Framework;
namespace ICSharpCode.Reporting.Test.DataSource
{
[TestFixture]
public class CollectionDataSourceFixture
{
ContributorCollection list;
[Test]
public void CollectionCountIsEqualToListCount() {
var collectionSource = new CollectionSource (list,typeof(Contributor),new ReportSettings());
Assert.That(collectionSource.Count,Is.EqualTo(list.Count));
}
[Test]
public void AvailableFieldsEqualContibutorsPropertyCount() {
var collectionSource = new CollectionDataSource (list,typeof(Contributor),new ReportSettings());
Assert.That(collectionSource.AvailableFields.Count,Is.EqualTo(6));
}
[Test]
public void GroupbyOneColumn () {
var rs = new ReportSettings();
rs.GroupColumnCollection.Add( new GroupColumn("GroupItem",1,ListSortDirection.Ascending));
var collectionSource = new CollectionDataSource (list,typeof(Contributor),rs);
collectionSource.Bind();
}
[Test]
public void GroupbyOneColumnAndFill () {
var ric = new System.Collections.Generic.List<IPrintableObject>(){
new BaseDataItem(){
ColumnName = "Lastname"
},
new BaseDataItem(){
ColumnName = "Firstname"
},
new BaseDataItem(){
ColumnName = "GroupItem"
},
new BaseDataItem(){
ColumnName = "RandomInt"
}
};
var rs = new ReportSettings();
rs.GroupColumnCollection.Add( new GroupColumn("GroupItem",1,ListSortDirection.Ascending));
rs.GroupColumnCollection.Add( new GroupColumn("RandomInt",1,ListSortDirection.Ascending));
var collectionSource = new CollectionDataSource (list,typeof(Contributor),rs);
collectionSource.Bind();
int i = 0;
do {
collectionSource.Fill(ric);
Console.WriteLine("first : <{0}> Last <{1}> Group <{2}> Randomint <{3}>",((BaseDataItem)ric[0]).DBValue,
((BaseDataItem)ric[1]).DBValue,
((BaseDataItem)ric[2]).DBValue,
((BaseDataItem)ric[3]).DBValue);
i ++;
}while (collectionSource.MoveNext());
Assert.That(i,Is.EqualTo(collectionSource.Count));
}
[SetUp]
public void CreateList() {
var contributorList = new ContributorsList();
list = contributorList.ContributorCollection;
}
}
}
Loading…
Cancel
Save