algorithm - Add sequence number for dynamic hierarchy in C# -
suppose have these records:
code |grouplevel |group ----------------------------------- x0000 |4 | x1000 |3 |x0000 x2000 |3 |x0000 x3000 |3 |x0000 x1100 |2 |x1000 x1200 |2 |x1000 x1300 |2 |x1000 x2100 |2 |x2000 x2200 |2 |x2000 x2300 |2 |x2000 x1110 |1 |x1100 x1120 |1 |x1100 x1111 |0 |x1110 x1112 |0 |x1110 x1113 |0 |x1110 x1114 |0 |x1110
what want acheive have kind of sequence number:
seq |code |grouplevel |group ------------------------------------- 1 |x0000 |4 | 2 |x1000 |3 |x0000 3 |x1100 |2 |x1000 4 |x1110 |1 |x1100 5 |x1111 |0 |x1110 6 |x1112 |0 |x1110 7 |x1113 |0 |x1110 8 |x1114 |0 |x1110 9 |x1120 |1 |x1100 10 |x1200 |2 |x1000 11 |x1300 |2 |x1000 12 |x2000 |3 |x0000 13 |x2100 |2 |x2000 14 |x2200 |2 |x2000 15 |x2300 |2 |x2000 16 |x3000 |3 |x0000
i tried fixed group level (3) not if group level > 5.
here's did:
list<mysequencemodel> _lstpair = new list<mysequencemodel>(); var _lst = _records.where(x => x.grouplevel == 3).tolist(); foreach (var item in _lst) { if (_lstpair.where(x => x.code.equals(item.code)).firstordefault() == null) _lstpair.add(new mysequencemodel { code = item.code, seqno = _seqcounter++ }); var _lst2 = _records.where(x => x.group.equals(item.code) && !x.code.equals(item.code)).orderby(x => x.code).tolist(); foreach (var _item2 in _lst2) { if (_lstpair.where(x => x.code.equals(_item2.code)).firstordefault() == null) _lstpair.add(new mysequencemodel { code = _item2.code, seqno = _seqcounter++ }); var _lst3 = _records.where(x => x.group.equals(_item2.code) && !x.code.equals(_item2.code)).orderby(x => x.code).tolist(); foreach (var _item3 in _lst3) { if (_lstpair.where(x => x.code.equals(_item3.code)).firstordefault() == null) _lstpair.add(new mysequencemodel { code = _item3.code, seqno = _seqcounter++ }); var _lst4 = _records.where(x => x.group.equals(_item3.code) && !x.code.equals(_item3.code)).orderby(x => x.code).tolist(); foreach (var _item4 in _lst4) { if (_lstpair.where(x => x.code.equals(_item4.code)).firstordefault() == null) _lstpair.add(new mysequencemodel { code = _item4.code, seqno = _seqcounter++ }); } } } }
mysequencemodel
class has code , sequence number needed reporting. pseudo code do.
tia
given following input data (take question):
mysequencemodel[] _records = new mysequencemodel[] { new mysequencemodel { code = "x0000", grouplevel = 4, group = "" }, new mysequencemodel { code = "x1000", grouplevel = 3, group = "x0000" }, new mysequencemodel { code = "x2000", grouplevel = 3, group = "x0000" }, new mysequencemodel { code = "x3000", grouplevel = 3, group = "x0000" }, new mysequencemodel { code = "x1100", grouplevel = 2, group = "x1000" }, new mysequencemodel { code = "x1200", grouplevel = 2, group = "x1000" }, new mysequencemodel { code = "x1300", grouplevel = 2, group = "x1000" }, new mysequencemodel { code = "x2100", grouplevel = 2, group = "x2000" }, new mysequencemodel { code = "x2200", grouplevel = 2, group = "x2000" }, new mysequencemodel { code = "x2300", grouplevel = 2, group = "x2000" }, new mysequencemodel { code = "x1110", grouplevel = 1, group = "x1100" }, new mysequencemodel { code = "x1120", grouplevel = 1, group = "x1100" }, new mysequencemodel { code = "x1111", grouplevel = 0, group = "x1110" }, new mysequencemodel { code = "x1112", grouplevel = 0, group = "x1110" }, new mysequencemodel { code = "x1113", grouplevel = 0, group = "x1110" }, new mysequencemodel { code = "x1114", grouplevel = 0, group = "x1110" }, };
then works:
var lookup = _records.tolookup(x => x.group); func<string, ienumerable<mysequencemodel>> traverse = null; traverse = grp => lookup[grp].selectmany(x => new [] { x }.concat(traverse(x.code))); mysequencemodel[] results = traverse("") .select((x, n) => new mysequencemodel { seqno = n + 1, code = x.code, grouplevel = x.grouplevel, group = x.group }) .toarray();
it gives me:
Comments
Post a Comment