none
How to return items in the right order? RRS feed

  • Question

  • Hi everybody,

    I have the following LINQ query:

    var items = _itemAdapter.GetAllList().Where(i => modifierGroup.ModGrpLnk.Any(m => m.ItemId == i.ItemId)).ToList();

    modifierGroup variable has a collection of ModGrpLnk entries, each of them have ItemId and SortOrder. 

    The goal is to return items attached to the modifier group in the Sort Order.

    I don't see what do I need to change in the above code to achieve that? May be I should first grab the assigned items and then somehow re-arrange them?

    Or somehow I should start from the modifierGroup.MogGrpLnk and from them get the items?

    Thanks a lot in advance for help.



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Friday, July 7, 2017 5:25 PM

Answers

  • I fixed the problem using the JOIN query. Don't know why I used the bad query originally - may be didn't realize that SortOrder was important.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Naomi N Friday, July 7, 2017 8:09 PM
    Friday, July 7, 2017 8:06 PM

All replies

  • Hi everybody,

    I have the following LINQ query:

    var items = _itemAdapter.GetAllList().Where(i => modifierGroup.ModGrpLnk.Any(m => m.ItemId == i.ItemId)).ToList();

    modifierGroup variable has a collection of ModGrpLnk entries, each of them have ItemId and SortOrder. 

    The goal is to return items attached to the modifier group in the Sort Order.

    I don't see what do I need to change in the above code to achieve that? May be I should first grab the assigned items and then somehow re-arrange them?

    Or somehow I should start from the modifierGroup.MogGrpLnk and from them get the items?

    Thanks a lot in advance for help.



    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    hi,

    >> The goal is to return items attached to the modifier group in the Sort Order.

    Did you tried to use OrderBy ?

    * replace the "FirstName" with the column you want to sort by

    // Use OrderBy
    
    var items = _itemAdapter.GetAllList().Where(i => modifierGroup.ModGrpLnk.Any(m => m.ItemId == i.ItemId)).OrderBy(x => x.FirstName).ToList();
    
    // or OrderByDescending
    
    var items = _itemAdapter.GetAllList().Where(i => modifierGroup.ModGrpLnk.Any(m => m.ItemId == i.ItemId)).OrderByDescending(x => x.FirstName).ToList();


    signature   Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]    [Linkedin]

    Friday, July 7, 2017 5:34 PM
  • Unfortunately, this is not what I am asking. I'm thinking I need to re-write the above query as a JOIN query. The problem with my current query is that it starts from the items and grabs the items that match the Modifier Group Links (the one that has the SortOrder column).

    So, there is no way to use OrderBy the way the query is currently written. It needs to be changed.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    • Edited by Naomi N Friday, July 7, 2017 6:22 PM
    Friday, July 7, 2017 6:21 PM
  • I fixed the problem using the JOIN query. Don't know why I used the bad query originally - may be didn't realize that SortOrder was important.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    • Marked as answer by Naomi N Friday, July 7, 2017 8:09 PM
    Friday, July 7, 2017 8:06 PM