2

Is there a way to simply convert an IEnumerable into an IOrderedEnumerable in O(1)?
I've seem this question which is close to mine, but haven't found what I'm looking for. By simply, I mean to avoid creating a new class just for this casting purpose like it was done in Servy's solution in the question I referred to.

Here is a the scenario as why I need such thing.

public IOrderedEnumerable<Output> Sort(IEnumerable<ComplexObjs> objs)
{
    return objs.OrderByDescending(o => o.A)
              .ThenBy(o => o.B)
              .ThenBy(o => o.C)
              .SelectMany(o => o.Childs.Select(x => new Output(o, x)))
}

Note : Everything is linq to object, there is nothing connected to a database in this scenario.

Community
  • 1
  • 1
AXMIM
  • 2,424
  • 1
  • 20
  • 38
  • What do you mean by, "avoid creating a new class just for this casting purpose"? What class? – Kirk Woll Jul 12 '16 at 20:05
  • I mean avoiding servy's solution in the question I refered. – AXMIM Jul 12 '16 at 20:06
  • 3
    Sounds like you don't know what the word "cast" means. Casting an object is simply informing the compiler that the object is in fact of a different type than what it thinks it is. Is your enumerable *actually* an `IOrderedEnumerable`? If not, then casting it will just fail at runtime. You want to know how to create a new object of type `IOrderedEnumerable`, and to do that, see my earlier answer. – Servy Jul 12 '16 at 20:08
  • I have to ask why you need to return an `IOrderedEnumerable`... do you intend to `.ThenBy` down the line? If not, what's wrong with a plain `IEnumerable`? – spender Jul 12 '16 at 20:08
  • @keyboardP, isn't public class NoopOrder a class for you? – AXMIM Jul 12 '16 at 20:09
  • @spender, there is no thenBy after that, but it at least tell that the result is ordered for the next user of this enumerable. – AXMIM Jul 12 '16 at 20:10
  • @servy, I didn't tried to cast it for this reason. Shall change the title to avoid confusion. – AXMIM Jul 12 '16 at 20:11
  • @AXMIM - I thought you were referring to the simple `OrderBy(a => 1);` result – keyboardP Jul 12 '16 at 20:12
  • 5
    @AXMIM Unless you specifically need to support `.ThenBy` to do secondary ordering, then I think you're misunderstanding the intent of an `IOrderedEnumerable`. Given that `IOrderedEnumerable` conveys nothing about the ordering criteria that were used, knowing that something is ordered without knowing the order is not all that helpful. – spender Jul 12 '16 at 20:13
  • @AXMIM If you aren't actually asking how to cast the variable, then yes, you should correct the quesiton to ask what you actually want to do, as currently what you're asking for appears to not actually be what you want. – Servy Jul 12 '16 at 20:14
  • @servy, well in my mind, the IEnumerable have already an order. Based on that, It should be feasible to simply use that order to have an IOrderedEnumerable. Anyway, I'm not against your previous answer, I just feeling like something was missing or wrong here. – AXMIM Jul 12 '16 at 20:24
  • @spender You've a point that I can't deny. It's still bug me a bit though – AXMIM Jul 12 '16 at 20:26
  • You can indeed do exactly that. I showed how to do so in my previous answer. Apparently this is a duplicate of that question, assuming you actually need this (which I doubt, as spender has explained). – Servy Jul 12 '16 at 20:26

0 Answers0