My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Copyright (c) 2009, Jardel Weyrich <jweyrich@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Linq;
using System.Linq.Expressions;

namespace System.Linq
{
public static class IQueryableOrderExtension
{
private static Expression<Func<T, object>> GetLambdaExpression<T>(this IQueryable<T> source, string property) {
// The 1st lambda's parameter (obj =>)
var param1 = Expression.Parameter(typeof(T), "obj");
// Split member's name
string[] properties = property.Split('.');
// The 2nd lambda's parameter (obj.member1.member2)
var param2 = Expression.Property(param1, properties[0]);
for (int i = 1; i < properties.Length; i++) {
param2 = Expression.Property(param2, properties[i]);
}
// Put it all together (obj => obj.member1.member2)
return Expression.Lambda<Func<T, object>>(
Expression.Convert(param2, typeof(object)), param1);
}

public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property, bool descending) {
var expression = GetLambdaExpression(source, property);
return (descending)
? source.OrderByDescending<T, object>(expression)
: source.OrderBy<T, object>(expression);
}

public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property) {
return OrderBy(source, property, false);
}

public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property, bool descending) {
var expression = GetLambdaExpression(source, property);
return (descending)
? source.ThenByDescending<T, object>(expression)
: source.ThenBy<T, object>(expression);
}

public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property) {
return ThenBy(source, property, false);
}
}
}
Show details Hide details

Change log

r21 by jweyrich on Oct 26, 2009   Diff
Argh! Forgot to change the filename.
Go to: 
Project members, sign in to write a code review

Older revisions

r19 by jweyrich on Oct 26, 2009   Diff
Refactored.
r18 by jweyrich on Oct 24, 2009   Diff
Added LINQ extension.
All revisions of this file

File info

Size: 2847 bytes, 68 lines
Hosted by Google Code