I have a function that I use to OrderBy() dynamically:
internal static Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> GetOrderBy(List<(string, bool)> orderColumnsAndIsDesc)
{
bool IsFirst = true;
MethodCallExpression resultExp = null;
string methodName;
LambdaExpression finalLambda = null;
foreach (var item in orderColumnsAndIsDesc)
{
string prop = item.Item1;
string orderType = item.Item2 == true ? "asc" : "desc";
Type typeQueryable = typeof(IQueryable<TEntity>);
ParameterExpression argQueryable = Expression.Parameter(typeQueryable, "p");
var outerExpression = Expression.Lambda(argQueryable, argQueryable);
IQueryable<TEntity> query = new List<TEntity>().AsQueryable<TEntity>();
Type type = typeof(TEntity);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
LambdaExpression lambda = Expression.Lambda(expr, arg);
if (IsFirst)
{
methodName = orderType == "asc" ? "OrderBy" : "OrderByDescending";
resultExp =
Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TEntity), type }, outerExpression.Body, Expression.Quote(lambda));
}
else
{
methodName = orderType == "asc" ? "ThenBy" : "ThenByDescending";
resultExp =
Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TEntity), type }, resultExp, Expression.Quote(lambda));
}
finalLambda = Expression.Lambda(resultExp, argQueryable);
IsFirst = false;
}
return (Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>)finalLambda.Compile();
}
It works correctly when there is one item to sort, but it gives an error when there is more than one item. its error:
System.InvalidOperationException: 'variable 'p' of type 'System.Linq.IQueryable'1[CMS.Data.Models.Category]' referenced from scope '', but it is not defined'
I don't know how to fix it, please help me.
Try this:
public static class IEnumerableExtensions {
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> source, params Tuple<string, bool>[] sortDefinitions)
=> OrderBy<T>(source, sortDefinitions.Select(i => new Tuple<Func<T, object>, bool>(GetPropertyLambda<T>(i.Item1), i.Item2)).ToArray());
//you can try use lambda direcly in params
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> source, params Tuple<Func<T, object>, bool>[] sortDefinitions) {
if(source?.Any() ?? false) {
var items = source;
foreach(var sortDefinition in sortDefinitions)
if(items is IOrderedEnumerable<T> ordered)
//thenby
items = sortDefinition.Item2 ? ordered.ThenBy(sortDefinition.Item1) : ordered.ThenByDescending(sortDefinition.Item1);
else
items = sortDefinition.Item2 ? items.OrderBy(sortDefinition.Item1) : items.OrderByDescending(sortDefinition.Item1);
return items;
} else
return source;
}
//based on your code
private static Func<T, object> GetPropertyLambda<T>(string propertyName) {
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
expr = Expression.Property(expr, pi);
return (Func<T, object>) Expression.Lambda(expr, arg).Compile();
}
}
If you use extension method ApplyOrderBy, you can write your function in the following way:
public static Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> GetOrderBy<TEntity>(
IEnumerable<(string, bool)> order)
{
return query => (IOrderedQueryable<TEntity>)query
.ApplyOrderBy(order.Select(t => Tuple.Create(t.Item1, t.Item2)));
}
I have a function like this:
public CountryDto FindCountryByName(string name)
{
Country country = _countryRepository.GetAll().Where(g => g.Name.ToLower().Trim() == name.ToLower().Trim()).FirstOrDefault();
var dto = _mapper.Map<Country, CountryDto>(country);
return dto;
}
and it's referred to GetAll-function in the GenericRepository
public IEnumerable<T> GetAll()
{
return table.ToList();
}
Is it possible creating a function like this (in the GenericRepository)?
public IEnumerable<T> FindByName(string objname, string name)
{
return table.Where(t => t.GetType(objname) == name);
}
By example
Country country = _countryRepository.FindByName("CountryName", name);
and
AlbumTrack track = _albumtrackRepository.FindByName("SongTitle", songTitle);
I don't remember where I found this function, I'm using it to generate LINQ queries based on given property name, please note that you can change the search function "Equals" to "Contains" or "StartsWith" :
///<summary>
/// Create query that search in given property of class T for matching results with value
/// </summary>
public static IQueryable<T> CreateSearchQuery<T>(IQueryable<T> queryable, string PropertyName, string value) where T : class
{
IQueryable<T> query = queryable;
List<Expression> expressions = new List<Expression>();
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
MethodInfo Equals_Method = typeof(string).GetMethod("Equals", new[] { typeof(string) });
MethodInfo ToString_Method = typeof(object).GetMethod("ToString");
//Iterate through all properties Except inherited ones
foreach (PropertyInfo prop in typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
.Where(x => x.PropertyType == typeof(string) //properties of type string
|| x.Name == PropertyName))
{
ConstantExpression value_expression = Expression.Constant(value, typeof(string));
MemberExpression member_expression = Expression.PropertyOrField(parameter, prop.Name);
//Combine ToString() and Equals() methods
MethodCallExpression callChain = Expression.Call(Expression.Call(member_expression, ToString_Method), Equals_Method, value_expression);
expressions.Add(callChain);
}
if (expressions.Count == 0)
return query;
Expression or_expression = expressions[0];
for (int i = 1; i < expressions.Count; i++)
{
or_expression = Expression.OrElse(or_expression, expressions[i]);
}
Expression<Func<T, bool>> expression = Expression.Lambda<Func<T, bool>>(or_expression, parameter);
return query.Where(expression);
}
I have to use Expression from LINQ to evaluate some data. I have created this small test program. The first example works find. It returns TRUE if any string is "abc".
public class ContainProgramClass
{
static void Main(string[] args)
{
ParameterExpression instance = Expression.Parameter(typeof(ContainClass), "item");
var mainClass = new ContainClass { Data = new List<string> { "abc", "def" } };
var expression = mainClass.CreateExpression(instance);
var lambda = Expression.Lambda<Func<ContainClass, bool>>(expression, instance);
Func<ContainClass, bool> f = lambda.Compile();
var result = f(mainClass);
Console.WriteLine($"Result: {result}"); // Result true
mainClass.Data = new List<string> { "foo", "bar" };
result = f(mainClass);
Console.WriteLine($"Result: {result}"); // Result false
Console.ReadKey();
}
}
public class ContainClass
{
private static readonly MethodInfo MethodContains = typeof(Enumerable).GetMethods(
BindingFlags.Static | BindingFlags.Public)
.Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2);
private static readonly MethodInfo EnumerableCastMethod = typeof(Enumerable).GetMethod("Cast");
private static MethodInfo GenericContainsMethod = MethodContains.MakeGenericMethod(typeof(object));
public List<string> Data { get; set; }
public Expression CreateExpression(Expression instance)
{
string propertyName = nameof(Data);
MemberExpression collectionPropertyAccessor = Expression.Property(instance, propertyName);
MethodCallExpression genericCollectionPropertyAccessor = Expression.Call(null
, EnumerableCastMethod.MakeGenericMethod(new[] { typeof(object) })
, collectionPropertyAccessor);
var distinctValueConstant = Expression.Constant("abc");
var containsExpression = Expression.Call(
ContainClass.GenericContainsMethod,
genericCollectionPropertyAccessor,
distinctValueConstant);
return containsExpression;
}
}
However, I would like for it to return TRUE if any SUBstring is "abc". Like this:
public class AnyProgramClass
{
static void Main(string[] args)
{
ParameterExpression instance = Expression.Parameter(typeof(ContainClass), "item");
var mainClass = new ContainClass { Data = new List<string> { "abcef", "ghi" } };
var expression = mainClass.CreateExpression(instance);
var lambda = Expression.Lambda<Func<ContainClass, bool>>(expression, instance);
Func<ContainClass, bool> f = lambda.Compile();
var result = f(mainClass);
Console.WriteLine($"Result: {result}"); // Result true
mainClass.Data = new List<string> { "abc", "bar" };
result = f(mainClass);
Console.WriteLine($"Result: {result}"); // Result true
mainClass.Data = new List<string> { "foo", "bar" };
result = f(mainClass);
Console.WriteLine($"Result: {result}"); // Result false
Console.ReadKey();
}
}
public class MainClass1
{
private static readonly MethodInfo MethodContains = typeof(Enumerable).GetMethods(
BindingFlags.Static | BindingFlags.Public)
.Single(m => m.Name == nameof(Enumerable.Contains) && m.GetParameters().Length == 2);
private static readonly MethodInfo MethodAny = typeof(Enumerable).GetMethods(
BindingFlags.Static | BindingFlags.Public)
.Single(m => m.Name == nameof(Enumerable.Any) && m.GetParameters().Length == 2);
private static readonly MethodInfo EnumerableCastMethod = typeof(Enumerable).GetMethod("Cast");
private static MethodInfo GenericContainsMethod = MethodContains.MakeGenericMethod(typeof(object));
private static MethodInfo GenericAnyMethod = MethodAny.MakeGenericMethod(typeof(object));
public List<string> Data { get; set; }
public Expression CreateExpression(Expression instance)
{
string propertyName = nameof(Data);
MemberExpression collectionPropertyAccessor = Expression.Property(instance, propertyName);
// would something like
// listOfStrings.Any(str => str.Contains("abc"))
return anyExpression;
}
}
I really have no idea on how to implement that Any(....). I usually get lost in the data types. Hopes some Expression-expert can lent me hand with this :)
You can do it for example like this:
string propertyName = nameof(Data);
MemberExpression collectionPropertyAccessor = Expression.Property(instance, propertyName);
var anyMethod = MethodAny.MakeGenericMethod(typeof(string));
Expression<Func<string, bool>> contains = x => x.Contains("abc");
return Expression.Call(anyMethod, collectionPropertyAccessor, contains);
Here you let compiler build expression you are passing to Any(..) call. If that doesn't suite your needs, you can build that expression manually:
string propertyName = nameof(Data);
MemberExpression collectionPropertyAccessor = Expression.Property(instance, propertyName);
var anyMethod = MethodAny.MakeGenericMethod(typeof(string));
// find string.Contains(string)
var containsMethod = typeof(string).GetMethods(BindingFlags.Instance | BindingFlags.Public).Single(c => c.Name == "Contains" && c.GetParameters().Length == 1 && c.GetParameters()[0].ParameterType == typeof(string));
// new parameter for sub-expression you pass to Any
ParameterExpression x = Expression.Parameter(typeof(string), "x");
// (string x) => x.Contains("abc") expression
Expression<Func<string, bool>> contains = Expression.Lambda<Func<string, bool>>(
Expression.Call(x, containsMethod, Expression.Constant("abc")), x);
return Expression.Call(anyMethod, collectionPropertyAccessor, contains);
I am using this snippet below for Ordering my Linq queries dynamically and works great. I am not great at reflection or complex linq queries but I need a way that when ascending order is used, that NULL values are last and vice versa.
So if my property name was an integer and the column values were 1, 3, 5, all NULL rows would be at the end, not at the beginning by default. What can I add to this expression to make that happen?
This code works with entity framework and still needs to for the NULL comparison.
Example
list.OrderBy("NAME DESC").ToList()
Class
public static class OrderByHelper
{
public static IOrderedQueryable<T> ThenBy<T>(this IEnumerable<T> enumerable, string orderBy)
{
return enumerable.AsQueryable().ThenBy(orderBy);
}
public static IOrderedQueryable<T> ThenBy<T>(this IQueryable<T> collection, string orderBy)
{
if (string.IsNullOrWhiteSpace(orderBy))
orderBy = "ID DESC";
IOrderedQueryable<T> orderedQueryable = null;
foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy, false))
orderedQueryable = ApplyOrderBy<T>(collection, orderByInfo);
return orderedQueryable;
}
public static IOrderedQueryable<T> OrderBy<T>(this IEnumerable<T> enumerable, string orderBy)
{
return enumerable.AsQueryable().OrderBy(orderBy);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> collection, string orderBy)
{
if (string.IsNullOrWhiteSpace(orderBy))
orderBy = "ID DESC";
IOrderedQueryable<T> orderedQueryable = null;
foreach (OrderByInfo orderByInfo in ParseOrderBy(orderBy, true))
orderedQueryable = ApplyOrderBy<T>(collection, orderByInfo);
return orderedQueryable;
}
private static IOrderedQueryable<T> ApplyOrderBy<T>(IQueryable<T> collection, OrderByInfo orderByInfo)
{
string[] props = orderByInfo.PropertyName.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
string methodName = String.Empty;
if (!orderByInfo.Initial && collection is IOrderedQueryable<T>)
{
if (orderByInfo.Direction == SortDirection.Ascending)
methodName = "ThenBy";
else
methodName = "ThenByDescending";
}
else
{
if (orderByInfo.Direction == SortDirection.Ascending)
methodName = "OrderBy";
else
methodName = "OrderByDescending";
}
return (IOrderedQueryable<T>)typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { collection, lambda });
}
private static IEnumerable<OrderByInfo> ParseOrderBy(string orderBy, bool initial)
{
if (String.IsNullOrEmpty(orderBy))
yield break;
string[] items = orderBy.Split(',');
foreach (string item in items)
{
string[] pair = item.Trim().Split(' ');
if (pair.Length > 2)
throw new ArgumentException(String.Format("Invalid OrderBy string '{0}'. Order By Format: Property, Property2 ASC, Property2 DESC", item));
string prop = pair[0].Trim();
if (String.IsNullOrEmpty(prop))
throw new ArgumentException("Invalid Property. Order By Format: Property, Property2 ASC, Property2 DESC");
SortDirection dir = SortDirection.Ascending;
if (pair.Length == 2)
dir = ("desc".Equals(pair[1].Trim(), StringComparison.OrdinalIgnoreCase) ? SortDirection.Descending : SortDirection.Ascending);
yield return new OrderByInfo() { PropertyName = prop, Direction = dir, Initial = initial };
initial = false;
}
}
private class OrderByInfo
{
public string PropertyName { get; set; }
public SortDirection Direction { get; set; }
public bool Initial { get; set; }
}
private enum SortDirection
{
Ascending = 0,
Descending = 1
}
It's relatively simple. For each passed sort selector, the method executes one of the following:
.OrderBy(x => x.Member)
.ThenBy(x => x.Member)
.OrderByDescending(x => x.Member)
.ThenByDescendiong(x => x.Member)
When the x.Member type is reference type or nullable value type, the desired behavior can be achieved by pre ordering with the same direction by the following expression
x => x.Member == null ? 1 : 0
Some people use ordering by bool, but I prefer to be explicit and use conditional operator with specific integer values. So the corresponding calls for the above calls would be:
.OrderBy(x => x.Member == null ? 1 : 0).ThenBy(x => x.Member)
.ThenBy(x => x.Member == null ? 1 : 0).ThenBy(x => x.Member)
.OrderByDescending(x => x.Member == null ? 1 : 0).ThenByDescending(x => x.Member)
.ThenByDescending(x => x.Member == null ? 1 : 0).ThenByDescending(x => x.Member)
i.e. the original method on the pre order expression followed by the ThenBy(Descending) with the original expression.
Here is the implementation:
public static class OrderByHelper
{
public static IOrderedQueryable<T> ThenBy<T>(this IEnumerable<T> source, string orderBy)
{
return source.AsQueryable().ThenBy(orderBy);
}
public static IOrderedQueryable<T> ThenBy<T>(this IQueryable<T> source, string orderBy)
{
return OrderBy(source, orderBy, false);
}
public static IOrderedQueryable<T> OrderBy<T>(this IEnumerable<T> source, string orderBy)
{
return source.AsQueryable().OrderBy(orderBy);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string orderBy)
{
return OrderBy(source, orderBy, true);
}
private static IOrderedQueryable<T> OrderBy<T>(IQueryable<T> source, string orderBy, bool initial)
{
if (string.IsNullOrWhiteSpace(orderBy))
orderBy = "ID DESC";
var parameter = Expression.Parameter(typeof(T), "x");
var expression = source.Expression;
foreach (var item in ParseOrderBy(orderBy, initial))
{
var order = item.PropertyName.Split('.')
.Aggregate((Expression)parameter, Expression.PropertyOrField);
if (!order.Type.IsValueType || Nullable.GetUnderlyingType(order.Type) != null)
{
var preOrder = Expression.Condition(
Expression.Equal(order, Expression.Constant(null, order.Type)),
Expression.Constant(1), Expression.Constant(0));
expression = CallOrderBy(expression, Expression.Lambda(preOrder, parameter), item.Direction, initial);
initial = false;
}
expression = CallOrderBy(expression, Expression.Lambda(order, parameter), item.Direction, initial);
initial = false;
}
return (IOrderedQueryable<T>)source.Provider.CreateQuery(expression);
}
private static Expression CallOrderBy(Expression source, LambdaExpression selector, SortDirection direction, bool initial)
{
return Expression.Call(
typeof(Queryable), GetMethodName(direction, initial),
new Type[] { selector.Parameters[0].Type, selector.Body.Type },
source, Expression.Quote(selector));
}
private static string GetMethodName(SortDirection direction, bool initial)
{
return direction == SortDirection.Ascending ?
(initial ? "OrderBy" : "ThenBy") :
(initial ? "OrderByDescending" : "ThenByDescending");
}
private static IEnumerable<OrderByInfo> ParseOrderBy(string orderBy, bool initial)
{
if (String.IsNullOrEmpty(orderBy))
yield break;
string[] items = orderBy.Split(',');
foreach (string item in items)
{
string[] pair = item.Trim().Split(' ');
if (pair.Length > 2)
throw new ArgumentException(String.Format("Invalid OrderBy string '{0}'. Order By Format: Property, Property2 ASC, Property2 DESC", item));
string prop = pair[0].Trim();
if (String.IsNullOrEmpty(prop))
throw new ArgumentException("Invalid Property. Order By Format: Property, Property2 ASC, Property2 DESC");
SortDirection dir = SortDirection.Ascending;
if (pair.Length == 2)
dir = ("desc".Equals(pair[1].Trim(), StringComparison.OrdinalIgnoreCase) ? SortDirection.Descending : SortDirection.Ascending);
yield return new OrderByInfo() { PropertyName = prop, Direction = dir, Initial = initial };
initial = false;
}
}
private class OrderByInfo
{
public string PropertyName { get; set; }
public SortDirection Direction { get; set; }
public bool Initial { get; set; }
}
private enum SortDirection
{
Ascending = 0,
Descending = 1
}
}
One approach is to pass an additional expression for testing for null into the method, and use it in an additional OrderBy/ThenBy clause.
Two OrderBy clauses would be produced - the first one will be on nullOrder, while the second one will be on the actual property.
private static IOrderedQueryable<T> ApplyOrderBy<T>(IQueryable<T> collection, OrderByInfo orderByInfo, Expression<Func<T,int>> nullOrder) {
...
if (!orderByInfo.Initial && collection is IOrderedQueryable<T>) {
if (orderByInfo.Direction == SortDirection.Ascending)
methodName = "ThenBy";
else
methodName = "ThenByDescending";
} else {
if (orderByInfo.Direction == SortDirection.Ascending)
methodName = "OrderBy";
else
methodName = "OrderByDescending";
}
if (nullOrder != null) {
collection = (IQueryable<T>)typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { collection, nullOrder });
// We've inserted the initial order by on nullOrder,
// so OrderBy on the property becomes a "ThenBy"
if (orderByInfo.Direction == SortDirection.Ascending)
methodName = "ThenBy";
else
methodName = "ThenByDescending";
}
// The rest of the method remains the same
return (IOrderedQueryable<T>)typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { collection, lambda });
}
The caller would need to pass a null checker explicitly. Passing null for non-nullable fields should work. You can construct them once, and pass as needed:
static readonly Expression<Func<string,int>> NullStringOrder = s => s == null ? 1 : 0;
static readonly Expression<Func<int?,int>> NullIntOrder = i => !i.HasValue ? 1 : 0;
static readonly Expression<Func<long?,int>> NullLongOrder = i => !i.HasValue ? 1 : 0;
My approach is to create a generic class that implements IComparer<TClass>. This way you can use your class in all LINQ statements with a non-default comparer. The advantage is that you will have full type checking at compile time. You can't name properties that can't be compared or that can't be null
class NullValueLastComparer<TClass, TKey> : IComparer<TClass>
where TClass : class
where TKey : IComparable<TKey>
{
This generic class has two Type parameters: the class that you want to compare, and the type of the property you want to compare with. The where clauses assert that TClass is a reference type, so you can access Properties, and TKey is something that implements normal comparison.
To create objects for the class we have two Factory functions. Both functions need a KeySelector, similar to lots of Key Selectors you can find in LINQ. The KeySelector function is the function that will tell you which property must be used in your comparisons. It is similar to the KeySelector in function Enumerable.Where.
The second Create function gives you the possibility to provide a non-default comparer, again similar to a lot of functions in the Enumerable class:
public static IComparer<TClass> Create(Func<TClass, TKey> keySelector)
{ // call the other Create function, with the default TKey comparer
return Create(keySelector, Comparer<TKey>.Default);
}
public static IComparer<TClass> Create(Func<TClass, TKey> keySelector, IComparer<TKey> comparer)
{ // construct a null value last comparer object
// initialize with the key selector and the key comparer
return new NullValueLastComparer<TClass, TKey>()
{
KeySelector = keySelector,
KeyComparer = comparer,
};
}
I use a private constructor. Only the static create classes can construct the null value last comparer
private NullValueLastComparer() { }
Two properties: the key selector and the comparer:
private Func<TClass, TKey> KeySelector { get; set; }
private IComparer<TKey> KeyComparer { get; set; }
The actual compare function. It will use the KeySelector to get the values
that must be compared, and compares them such that a null value will be last.
public int Compare(TClass x, TClass y)
{
if (Object.ReferenceEquals(x, null))
throw new ArgumentNullException(nameof(x));
if (Object.ReferenceEquals(y, null)
throw new ArgumentNullException(nameof(y));
// get the values to compare
TKey keyX = KeySelector(x);
TKey keyY = KeySelector(y);
return this.Compare(keyX, keyY);
}
The private function that compares the Keys such that null values will be last
private int Compare(TKey x, TKey y)
{ // compare such that null values last, or if both not null, use IComparable
if (Object.ReferenceEquals(x, null))
{
if (Object.ReferenceEquals(y, null))
{ // both null
return 0;
}
else
{ // x null, y not null => x follows y
return +1;
}
}
else
{ // x not null
if (Object.ReferenceEquals(y, null))
{ // x not null; y null: x precedes y
return -1;
}
else
{
return this.KeyComparer.Compare(x, y);
}
}
}
}
Usage:
class Person
{
public string FirstName {get; set;}
public string FamilyName {get; set;}
}
// create a comparer that will put Persons without firstName last:
IComparer<Person> myComparer =
NullValueLastComparer<Person, string>.Create(person => person.FirstName);
Person person1 = ...;
Person person2 = ...;
int compareResult = myComparer.Compare(person1, person2);
This compare will compare Persons. When two Persons are compared, it will take person.FirstName for both persons, and will put the one without FirstName as last.
Usage in a complicated LINQ statement.
Note that there is full type checking at compile time.
IEnumerable<Person> myPersonCollection = ...
var sortedPersons = myPersonCollection
.OrderBy(person => person, myComparer)
.ThenBy(person => person.LastName)
.Select(person => ...)
.ToDictonary(...)
For dynamically constructed Order By expression like this list.OrderBy("NAME DESC").ToList(), you can use the following query helper extension method.
Usage
First of all, we check to make sure property name exists in the given Class. If we do not check, it'll throw run-time exception.
Then we use use either OrderByProperty or OrderByPropertyDescending.
string orderBy = "Name";
if (QueryHelper.PropertyExists<User>(orderBy))
{
list = list.OrderByProperty(orderBy);
- OR -
list = list.OrderByPropertyDescending(orderBy);
}
Here is the real world usage in my project at GitHub.
Query Helper
public static class QueryHelper
{
private static readonly MethodInfo OrderByMethod =
typeof (Queryable).GetMethods().Single(method =>
method.Name == "OrderBy" && method.GetParameters().Length == 2);
private static readonly MethodInfo OrderByDescendingMethod =
typeof (Queryable).GetMethods().Single(method =>
method.Name == "OrderByDescending" && method.GetParameters().Length == 2);
public static bool PropertyExists<T>(string propertyName)
{
return typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.Instance) != null;
}
public static IQueryable<T> OrderByProperty<T>(
this IQueryable<T> source, string propertyName)
{
if (typeof (T).GetProperty(propertyName, BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.Instance) == null)
{
return null;
}
ParameterExpression paramterExpression = Expression.Parameter(typeof (T));
Expression orderByProperty = Expression.Property(paramterExpression, propertyName);
LambdaExpression lambda = Expression.Lambda(orderByProperty, paramterExpression);
MethodInfo genericMethod =
OrderByMethod.MakeGenericMethod(typeof (T), orderByProperty.Type);
object ret = genericMethod.Invoke(null, new object[] {source, lambda});
return (IQueryable<T>) ret;
}
public static IQueryable<T> OrderByPropertyDescending<T>(
this IQueryable<T> source, string propertyName)
{
if (typeof (T).GetProperty(propertyName, BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.Instance) == null)
{
return null;
}
ParameterExpression paramterExpression = Expression.Parameter(typeof (T));
Expression orderByProperty = Expression.Property(paramterExpression, propertyName);
LambdaExpression lambda = Expression.Lambda(orderByProperty, paramterExpression);
MethodInfo genericMethod =
OrderByDescendingMethod.MakeGenericMethod(typeof (T), orderByProperty.Type);
object ret = genericMethod.Invoke(null, new object[] {source, lambda});
return (IQueryable<T>) ret;
}
}
I'm using this search function. but I need it to do an "and" not an "or" I cant seem to get it to return the results I want. I need to perform a search function where the search results match the text entered in box. But only a partial search. For example if I type in "Super D" I want it to find everything that contains a "Super" AND a "D".
public static class ObjectContextExtensions
{
public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey)
{
return FullTextSearch<T>(queryable, searchKey, false);
}
public static IQueryable<T> FullTextSearch<T>(this IQueryable<T> queryable, string searchKey,
bool exactMatch)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "c");
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
// MethodInfo toStringMethod = typeof (object).GetMethod("ToString", new Type[] {});
var publicProperties =
typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => p.PropertyType == typeof(string));
Expression orExpressions = null;
string[] searchKeyParts;
if (searchKey == null)
{
searchKey = "0";
}
searchKeyParts = !exactMatch ? searchKey.Split(' ') : new[] { searchKey };
foreach (MethodCallExpression callContainsMethod in from property in publicProperties
select Expression.Property(parameter, property) into nameProperty
from searchKeyPart in searchKeyParts
let searchKeyExpression = Expression.Constant(searchKeyPart) let containsParamConverted = Expression.Convert(searchKeyExpression, typeof(string))
select Expression.Call(nameProperty, containsMethod, (Expression)containsParamConverted))
{
if (orExpressions == null)
{
orExpressions = callContainsMethod;
}
else
{
orExpressions = Expression.Or(orExpressions,callContainsMethod);
}
}
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryable.ElementType },
queryable.Expression,
Expression.Lambda<Func<T, bool>>(orExpressions, new ParameterExpression[] { parameter }));
return queryable.Provider.CreateQuery<T>(whereCallExpression);
}
}
Looks as though setting exactMatch to true would do the trick as it wouldn't split the search terms up.
FullTextSearch<MyType>(searchKey, true)
Failing that, change
orExpressions = Expression.Or(orExpressions,callContainsMethod);
to
andExpressions = Expression.And(andExpressions,callContainsMethod);