C# LINQ Group By Base Class -


i build base class keeps primary keys like:

public class primarykey {            [key]     [column(order = 1)]     public datetime date { get; set; }      [key]     [column(order = 2)]     public int id1 { get; set; }      [key]     [column(order = 3)]     public int id2 { get; set; } } 

and more classes different kind of data derived class.

then want use 1 method create lists these derived class groupby primary keys. have done right is:

private ienumerable<igrouping<primarykey, t>> datagroupbyprimarykey<t>(ienumerable<t> source) t : primarykey     {         return source.groupby(s => new primarykey         {             date = s.date,             id1 = s.id1,             id2 = s.id2         });     } 

but looks not working since after program runs through method, lists same set of primary keys still keep ungrouped.

i experimented like

source.groupby(s => new         {             s.date,             s.id1,             s.id2         }); 

and make data grouped, since groupby type anonymous not suitable method.

is there wrong in original method write?

[edited , more information added]

sorry not describing question clearly. doing copying data different databases, , each row should unique set of these keys. (then set called primary key)

in original data, there rows same primary key set. after groupby process, sum data same primary key set, , make dictionary.

sourceaftergroupby.select(s => new derivedclasswithdata         {             date = s.key.date,             id1 = s.key.id1,             id2 = s.key.id2,                             data1 = s.sum(p => p.data1),             data2 = s.sum(p => p.data2)         });  datasum.todictionary(s => s.primarykeytuple); 

the problem right is, if use anonymous type in groupby function, can surely group data key set. if want use primarykey type, after method still ungrouped, , wonder why happened.

let primarykey implement iequable interface , override object.gethashcode method. code like:

public class primarykey : iequable<primarykey> {            // other properties     bool equals(primarykey other)     {         return this.date == other.date && this.id1 == other.id1 && this.id2 == other.id2;     }      override int gethashcode()     {         return this.date.gethashcode() ^ this.id2.gethashcode() ^ this.id2.gethashcode();     } } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -