Browse Source

Sort Grouping with Linq and Fill Items

reports
Peter Forstmeier 12 years ago
parent
commit
f99845489c
  1. 134
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/CollectionSource.cs
  2. 6
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/IndexList.cs
  3. 11
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/CollectionHandlingFixture.cs
  4. 6
      src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/ContributorsList.cs

134
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/CollectionSource.cs

@ -65,6 +65,8 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -65,6 +65,8 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
}
}
protected object Current_test {get;private set;}
public object Current {
get {
return baseList[((BaseComparer)IndexList[CurrentPosition]).ListIndex];
@ -144,23 +146,40 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -144,23 +146,40 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
public void Fill_Test (List<IPrintableObject> collection) {
var currentKey = groupEnumerator.Current;
Console.WriteLine("{0} - {1}",currentKey.Key,currentKey.Count());
Console.WriteLine("{0}",currentKey.Key);
var z = listEnumerator.Current;
Console.WriteLine("\t...{0} - {1}",((BaseComparer)z).ListIndex.ToString(),
((BaseComparer)z).ObjectArray[0].ToString());
foreach (IPrintableObject item in collection)
{
var dbItem = item as IDataItem;
if (dbItem != null) {
dbItem.DBValue = String.Empty;
var p = listProperties.Find(dbItem.ColumnName,true);
dbItem.DBValue = p.GetValue(Current_test).ToString();
if (String.IsNullOrEmpty(dbItem.DataType)) {
dbItem.DataType = p.PropertyType.ToString();
}
}
}
}
public bool MoveNext_Test_List() {
var canMove = listEnumerator.MoveNext();
if (! canMove) {
if (! canMove) {
var groupCanMove = groupEnumerator.MoveNext();
if (groupCanMove) {
listEnumerator = groupEnumerator.Current.GetEnumerator();
canMove = listEnumerator.MoveNext();
// Current_test = listEnumerator.Current;
Current_test = baseList[listEnumerator.Current.ListIndex];
} else {
Console.WriteLine("end");
}
} else {
Current_test = baseList[listEnumerator.Current.ListIndex];
}
return canMove;
}
@ -171,72 +190,39 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -171,72 +190,39 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
#region Grouping
public void Group()
{
var sortedList = BuildSortIndex(baseList,reportSettings.GroupColumnCollection);
var unsortedList = this.BuildIndexInternal(baseList,reportSettings.GroupColumnCollection);
Console.WriteLine("GroupBy() _ 0");
// var grouped_x = unsortedList.GroupBy(a => a.ObjectArray[0]);
/*
var grouped_x = GroupTestOne(unsortedList);
foreach (var element in grouped_x) {
Console.WriteLine("{0} - {1} ",element.Key.ToString(),element.ToString());
foreach (var xx in element) {
Console.WriteLine("...{0}",((BaseComparer)xx).ObjectArray[0].ToString());
}
}
*/
Console.WriteLine("GroupBy() _ 1");
// var groupByLinq = (from car in unsortedList
// group car by car.ObjectArray[0]);
// foreach (var element in groupByLinq) {
// Console.WriteLine("{0} - {1} ",element.Key.ToString(),element.Key is BaseComparer);
// foreach (var xx in element) {
// Console.WriteLine("...{0}",((BaseComparer)xx).ObjectArray[0].ToString());
// }
// }
// IEnumerable<IGrouping<BaseComparer, BaseComparer>> xx = unsortedList.GroupBy(a => a.ObjectArray[0]);
IEnumerable<IGrouping<object, BaseComparer>> grouped = unsortedList.GroupBy(a => a.ObjectArray[0]);;
Console.WriteLine("GroupBy() _ 2");
foreach (var element in grouped) {
Console.WriteLine("{0} - {1} ",element.Key.ToString(),element.Key is BaseComparer);
foreach (var xx in element) {
Console.WriteLine("...{0}",((BaseComparer)xx).ObjectArray[0].ToString());
}
}
ShowIndexList(unsortedList);
// newList = GroupTestOne(unsortedList);
newList = GroupTestLinq(unsortedList);
newList = GroupTestLinq(sortedList);
ShowGrouping(newList);
groupEnumerator = newList.GetEnumerator();
groupEnumerator.MoveNext();
listEnumerator = groupEnumerator.Current.GetEnumerator();
listEnumerator.MoveNext();
var z = listEnumerator.Current;
// Current_test = listEnumerator.Current;
// return baseList[((BaseComparer)IndexList[CurrentPosition]).ListIndex];
Current_test = baseList[listEnumerator.Current.ListIndex];
Console.WriteLine("--------Display output-----");
}
// http://stackoverflow.com/questions/5013710/linq-order-by-group-by-and-order-by-each-group?rq=1
IEnumerable<IGrouping<object, BaseComparer>> GroupTestOne (IndexList list) {
IEnumerable<IGrouping<object, BaseComparer>> GroupOrderByRandomInt (IndexList list) {
SortColumnCollection sc = new SortColumnCollection();
sc.Add(new SortColumn("RandomInt", ListSortDirection.Ascending));
return list.GroupBy(a => a.ObjectArray[0]);
}
IEnumerable<IGrouping<object, BaseComparer>> GroupTestLinq (IndexList list) {
return (from car in list
group car by car.ObjectArray[0]);
group car by car.ObjectArray[0]);
}
/*
void Group_test(IndexList unsortedList)
{
@ -286,31 +272,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -286,31 +272,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
}
}
*/
/*
private Dictionary<string,IndexList> BuildGroup_1 (IndexList list,GroupColumnCollection groups) {
var dictionary = new Dictionary<string,IndexList>();
PropertyDescriptor[] groupProperties = BuildSortProperties (groups);
foreach (var element in list) {
string groupValue = ExtractValue (element,groupProperties);
if (!dictionary.ContainsKey(groupValue)) {
dictionary[groupValue] = new IndexList();
}
dictionary[groupValue].Add(element);
}
Console.WriteLine("Dictonary ");
foreach (var el in dictionary.Values) {
Console.WriteLine(el.Count.ToString());
foreach (var element in el) {
Console.WriteLine("-- {0}",element.ToString());
}
}
return dictionary;
}
*/
/*
private IndexList BuildGroup (IndexList source,GroupColumnCollection groups)
@ -344,6 +306,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -344,6 +306,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
}
*/
/*
void ShowGrouping(ref IndexList idlist)
{
Console.WriteLine("----ShowGrouping---");
@ -356,7 +319,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -356,7 +319,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
}
}
}
*/
string ExtractValue(BaseComparer element,PropertyDescriptor[] groupProperties)
{
@ -501,19 +464,24 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -501,19 +464,24 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
private static void ShowIndexList (IndexList list)
{
Console.WriteLine("ShowIndexList");
foreach (BaseComparer element in list) {
var groupComparer = element as GroupComparer;
if (groupComparer == null) continue;
if (groupComparer.IndexList.Any()) {
var ss = String.Format("{0} with {1} Children",element.ObjectArray[0],groupComparer.IndexList.Count);
System.Console.WriteLine(ss);
foreach (BaseComparer c in groupComparer.IndexList) {
Console.WriteLine("---- {0}",c.ObjectArray[0]);
}
}
Console.WriteLine("\t...{0} - {1}",((BaseComparer)element).ListIndex.ToString(),
((BaseComparer)element).ObjectArray[0].ToString());
}
}
static void ShowGrouping(IEnumerable<IGrouping<object, BaseComparer>> grouping)
{
Console.WriteLine("----------ShowGrouping-----------");
foreach (var element in grouping) {
Console.WriteLine("{0} - {1} ", element.Key.ToString(), element.Key is BaseComparer);
foreach (var xx in element) {
Console.WriteLine("...{0}", ((BaseComparer)xx).ObjectArray[0].ToString());
}
}
}
// static string WrongColumnName(string propertyName)
// {
// return String.Format(CultureInfo.InvariantCulture, "Error : <{0}> missing!", propertyName);

6
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/DataManager/Listhandling/IndexList.cs

@ -17,11 +17,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling @@ -17,11 +17,7 @@ namespace ICSharpCode.Reporting.DataManager.Listhandling
/// </summary>
public class IndexList :List<BaseComparer>
{
public IndexList()
{
}
public int CurrentPosition {get;set;}
}
}

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

@ -126,15 +126,26 @@ namespace ICSharpCode.Reporting.Test.DataSource @@ -126,15 +126,26 @@ namespace ICSharpCode.Reporting.Test.DataSource
},
new BaseDataItem(){
ColumnName = "Firstname"
},
new BaseDataItem(){
ColumnName = "GroupItem"
}
};
var rs = new ReportSettings();
rs.GroupColumnCollection.Add( new GroupColumn("GroupItem",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,
((BaseDataItem)ric[1]).DBValue,
((BaseDataItem)ric[2]).DBValue);
i ++;
}while (collectionSource.MoveNext_Test_List());
Assert.That(i,Is.EqualTo(collectionSource.Count));
}
/*

6
src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/DataSource/ContributorsList.cs

@ -26,7 +26,13 @@ namespace ICSharpCode.Reporting.Test.DataSource @@ -26,7 +26,13 @@ namespace ICSharpCode.Reporting.Test.DataSource
public ContributorCollection ContributorCollection {get; private set;}
// list.Add(new Contributor("Ifko","Kovacka","",31,d3,"A"));
// list.Add(new Contributor("Nathan","Allen","",5,d3,"A"));
// list.Add(new Contributor("Dickon","Field","DBTools",10,d3,"U"));
// list.Add(new Contributor("Roman","Taranchenko","",2,d2,"U"));
// list.Add(new Contributor("Denis","Erchoff","",13,d2,"U"));
private ContributorCollection CreateContributorsList () {
DateTime d1 = new DateTime(2000,11,11);

Loading…
Cancel
Save