0

It would be desirable to be able to provide e.g. comparison functions (i.e. with lambdas) for an anonymous type, so that they can be sorted by a set of criteria. Is that possible in C#?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 1
    You seem to ask two slightly different questions: one in the title, and one in the body. Which one do you want answered? – Evan Krall Apr 22 '11 at 03:31
  • @Evan: I think it's a hybrid of both: he wants to define *methods* for anonymous types in *some way*, which could be with lambda syntax or some other kind of syntax. They're asking the same thing even though they seem to be asking about different topics. – user541686 Apr 22 '11 at 03:37
  • @Mehrdad See, I interpret it as asking how to specify comparison functions for anonymously typed objects (hence the body), and Billy assumed that he would need to attach a comparator method to those objects to do so (hence the title) – Evan Krall Apr 22 '11 at 03:40
  • @Evan: How so? I see only one question. I provided an example where attaching the method might be useful (i.e. allowing comparisons), but it's still one question. – Billy ONeal Apr 22 '11 at 03:40
  • @Evan: Like Billy said, lambdas for sorting were an example of a potential solution, not the original problem. – user541686 Apr 22 '11 at 03:41
  • @Mehrdad Until he made that comment, it was unclear. Generally, if someone asks a question and gives 1) a problem and 2) a solution to the problem that may or may not work, he's probably asking about the problem. – Evan Krall Apr 22 '11 at 03:55
  • possible duplicate of [How do I define a method in an anonymous type?](http://stackoverflow.com/questions/713561/how-do-i-define-a-method-in-an-anonymous-type) – Jacob Krall Apr 22 '11 at 04:10
  • You marked strange reply as an answer, the correct one is as in linked post -- it IS possible via little trick, using fields as functors. For the outside world this looks exactly like a regular method. – greenoldman Apr 22 '11 at 05:14
  • @macias: No, that isn't true. C# might use the same sytax but at the CLR level they are very different. (And things expecting something like ICompareable won't work that way) – Billy ONeal Apr 22 '11 at 05:32

3 Answers3

4

No, just make a regular class instead.

Possible related: Can a C# anonymous class implements an interface?

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • That's what I thought, but wanted to be sure. +1. – Billy ONeal Apr 22 '11 at 02:45
  • It is, in fact, possible in C# to "provide e.g. comparison functions (i.e. with lambdas) for an anonymous type, so that they can be sorted by a set of criteria." Therefore, "No" is incorrect. – Evan Krall Apr 22 '11 at 03:30
  • @Evan: I think you missed the title. It's asking for a solution involving *attaching **methods***, not just any solution that involves delegates/lambdas. – user541686 Apr 22 '11 at 03:31
  • 2
    @Downvoters: You must be Java programmers...OP wants an anonymous type that implements some interface e.g. IComparable, in order to pass the instance to some sort method that takes IComparable as a parameter. In Java it's possible, in C# it's not allowed. – Cheng Chen Apr 22 '11 at 03:41
  • @Danny: Lol just curious, why? Do you mean because you can have methods in anonymous classes in Java, or something else? :) – user541686 Apr 22 '11 at 03:43
0

Lambdas are implicitly convertable to System.Comparison`1:

var anons = (new[] {new {a = 3}, new {a = 4}, new {a = 2}}).ToList();
anons.Sort((x, y) => (x.a - y.a));

You can also use the LINQ OrderBy extension method to sort anonymous types.

var anons = new[] {new {a = 3}, new {a = 4}, new {a = 2}};
var sorted = anons.OrderBy(s => s.a);
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
-2

Yes. You must declare the type of the comparison function:

var anon = new {comparator = (Func<string, int>) (s => s.Length)};
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76