none
Linq query for order an OsservableCollection RRS feed

  • Question

  • Hi to all the ML. I've a structure collection like this:

    Group	Owner		Pos   Comp
    AAAA	AAAA		101	2210
    AAAA	AAAA		202	2220
    AAAA	AAAA		301	5550
    AAAA	AAAA		301	5560
    AAAA	2210			101	6789
    AAAA	2220			202	ABABA
    AAAA	5550			301	XXXX0
    AAAA	5550			301	XXXX1
    AAAA	5560			301	YYYY0
    AAAA	5560			301	6612
    AAAA	6789			101	ZZZZZ.0
    AAAA	6789			101	ZZZZZ.1
    AAAA	6789			101	ZZZZZ.2
    AAAA	6789			101	ZZZZZ.3
    AAAA	ABABA		202	TTTTT.0
    AAAA	ABABA		202	TTTTT.1
    AAAA	ABABA		202	TTTTT.2
    AAAA	6612			301	ZZZZZ.0
    AAAA	6612			301	ZZZZZ.1
    AAAA	6612			301	ZZZZZ.2
    AAAA	6612			301	ZZZZZ.3
    AAAB	...	  		...	.....

    I need to it for have a result like this:

    Group	Owner		Pos   Comp
    AAAA	AAAA      	101	2210
    AAAA	2210	 	101	6789
    AAAA	6789		101	ZZZZZ.0
    AAAA	6789		101	ZZZZZ.1
    AAAA	6789		101	ZZZZZ.2
    AAAA	6789		101	ZZZZZ.3
    AAAA	AAAA	202	2220
    AAAA	2220		202	ABABA
    AAAA	ABABA	202	TTTTT.0
    AAAA	ABABA	202	TTTTT.1
    AAAA	ABABA	202	TTTTT.2
    AAAA	AAAA      	301	5550
    AAAA	5550      	301	XXXX0
    AAAA	5550	  	301	XXXX1
    AAAA	AAAA      	301	5560
    AAAA	5560	  	301	YYYY0
    AAAA	5560	  	301	6612
    AAAA	6612	  	301	ZZZZZ.0
    AAAA	6612	  	301	ZZZZZ.1
    AAAA	6612	  	301	ZZZZZ.2
    AAAA	6612	 	301	ZZZZZ.3
    AAAB	...	  	...	.....
    It's possibile with linq? Thanks to all.







    • Edited by Jotric1978 Tuesday, April 10, 2012 8:19 AM
    Tuesday, April 10, 2012 8:12 AM

Answers

  • Hi jotric1,

    Please try the code below.

    var query = from t in context.Text group t by t.Group into g
                select new {textEntity = g.OrderBy(x=>x.Pos)};
    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 17, 2012 7:04 AM
    Moderator

All replies

  • Hi jotric1,

    You can use the OrderBy() and ThenBy() methods to order your data by multiple fields, or using query syntax it'd be orderby field1, field2, etc. Judging by your example what you'd want would be the following in Method syntax:

    var orderedData = data.OrderBy(d => d.Group)
                          .ThenBy(d => d.POS)
                          .ThenBy(d => d.Comp)
                          .ThenBy(d => d.Owner);

    and in query syntax:

    var orderedData = from d in data
                      orderby d.Group, d.POS, d.Comp, d.Owner
                      select d;

    Regards,

    Tyler

    • Proposed as answer by Patrice ScribeMVP Tuesday, April 10, 2012 11:48 AM
    • Unproposed as answer by Jotric1978 Wednesday, April 11, 2012 6:45 AM
    Tuesday, April 10, 2012 9:38 AM
  • Thanks for your reply, but I have not solved the problem.

    With this kind of order, I have not the result I need.The problem is that I want to order by gruop, then where in the comp I find some there's in the owner, put in the lines after the main owner the relative component, then order by owner and in the last order by position.

    First->order by group

    second->find if in the comp column I'have some correspondences with the owner then, put in the lines after the owner of the component I've in the line before

    third->order by pos

    Wednesday, April 11, 2012 7:10 AM
  • Hi jotric1,

    This should give you the hierarchal ordering you need:

                var orderedData =(from d in data
                                  group d by d.Group into g
                                  orderby g.Key
                                  from d in g
                                  select new[] {d}.Union(g.Where(c => c.Owner == d.Comp)) into withChildren
                                  from wc in withChildren
                                  orderby wc.POS
                                  select wc).Distinct();

    Regards,

    Tyler

    • Proposed as answer by Tyler_A Wednesday, April 11, 2012 10:50 AM
    Wednesday, April 11, 2012 10:01 AM
  • Ok. Thank you very very much. I've tried it into LINQPad and seems it works fine.

    I've another question, if I need to set a Where statement (where group=XXXX) I must to put it before the first group filter. It's correct?

    Now I need to make a VB.Net conversion....

    Wednesday, April 11, 2012 10:35 AM
  • Yes, you'd want to place the Where before the group... by...

    Regards,

    Tyler

    • Marked as answer by Jotric1978 Wednesday, April 11, 2012 10:59 AM
    • Unmarked as answer by Jotric1978 Friday, April 13, 2012 1:18 PM
    Wednesday, April 11, 2012 10:50 AM
  • I've tried with a new collection, and this dosn't work.

    Group	Owner	Pos   Comp
    AAAA	AAAA      	101	2210
    AAAA	2210	 	101	6789
    AAAA	6789		101	ZZZZZ.0
    AAAA	6789		101	ZZZZZ.1
    AAAA	6789		101	ZZZZZ.2
    AAAA	6789		101	ZZZZZ.3
    AAAA	AAAA	202	2220
    AAAA	2220		202	ABABA
    AAAA	ABABA	202	TTTTT.0
    AAAA	ABABA	202	TTTTT.1
    AAAA	ABABA	202	TTTTT.2
    AAAA	AAAA      	301	5550
    AAAA	5550      	301	XXXX0
    AAAA	5550	  	301	XXXX1
    AAAA	AAAA      	301	5560
    AAAA	5560	  	301	YYYY0
    AAAA	5560	  	301	6612
    AAAA	6612	  	301	ZZZZZ.0
    AAAA	6612	  	301	ZZZZZ.1
    AAAA	6612	  	301	ZZZZZ.2
    AAAA	6612	 	301	ZZZZZ.3
    AAAB	BBBB        501  A6666
    AAAB       BBBB       101  A7777
    AAAB       BBBB       301  A5666
    AAAB       A6666     101   3666
    AAAB       A6666     101   3667
    AAAB       A6666     101   3668
    If I add at the collection a new group... I've a result that is ordered only by owner.
    Friday, April 13, 2012 1:29 PM
  • Hi jotric1,

    Welcome to MSDN Forum.

    I've tested @Tyler_A's solution, it works well, what's the meaning of "add at the collection a new group"? I suggest you to check the T-SQL which generated by the Linq query from SQL Profiler, make sure whether it is what you want.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 16, 2012 7:39 AM
    Moderator
  • In the first version of the collectin at the Group column I've all the entries that starts with "AAAA". In the second version, I've add some entries with the "AAAB".I've made some test with LINQPad and the final result isn't what I wont. I need to order the results first for the Group column,inside of each group I need to order by the pos colum.

    Monday, April 16, 2012 9:02 AM
  • Hi jotric1,

    Please try the code below.

    var query = from t in context.Text group t by t.Group into g
                select new {textEntity = g.OrderBy(x=>x.Pos)};
    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 17, 2012 7:04 AM
    Moderator