How to cache the following variable that holds class properties - c#

I have the following functionality that searches string based properties on a class for a text match, I would like to cache the 'properties' variable in Match() so it doesn't get refreshed every time I run through a list of class objects.
public bool Match<T>(T item, string searchTerm)
{
//You should cache the results of properties here for max perf.
IEnumerable<Func<T, string>> properties = GetPropertyFunctions<T>();
bool match = properties.Select(prop => prop(item)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
public IEnumerable<Func<T, string>> GetPropertyFunctions<T>()
{
var stringProperties = GetStringPropertyFunctions<T>();
var decimalProperties = GetDecimalPropertyFunctions<T>();
return stringProperties.Concat(decimalProperties);
}
public IEnumerable<Func<T, string>> GetStringPropertyFunctions<T>()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(string)).ToList();
var properties = propertyInfos.Select(GetStringPropertyFunc<T>);
return properties;
}
public Func<T, string> GetStringPropertyFunc<T>(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, string>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, string>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, string> propertyAccessor = expression.Compile();
return propertyAccessor;
}
public IEnumerable<Func<T, string>> GetDecimalPropertyFunctions<T>()
{
var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(decimal)).ToList();
var properties = propertyInfos.Select(GetDecimalPropertyFunc<T>);
return properties;
}
public Func<T, string> GetDecimalPropertyFunc<T>(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(T), "x");
Expression<Func<T, decimal>> expression = System.Linq.Expressions.Expression.Lambda<Func<T, decimal>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<T, decimal> propertyAccessor = expression.Compile();
return (T item) => propertyAccessor(item).ToString();
}

public static class FullTextSearch<T>
{
private List<Func<T, string>> _properties;
static FullTextSearch()
{
_properties = GetPropertyFunctions<T>().ToList();
}
public bool Match(T item, string searchTerm)
{
bool match = _properties.Select(prop => prop(item)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
}

Make properties a private field and use the Lazy Wrapper to initialize it on first use.
You have to make the other methods static to use it like this, but that should be easily possible.
private Lazy<IEnumerable<Func<T, string>>> properties = new Lazy<IEnumerable<Func<T, string>>>(GetPropertyFunctions<T>);
public bool Match<T>(T item, string searchTerm)
{
bool match = properties.Value.Select(prop => prop(item)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}

Related

Linq - How to query Dictionary<int,object>

I want to be able to query and sort a Dictionary<int, MyObj> by any of the properties inside MyObj.
class MyObj
{
public string Symbol { get; set; }
public string Name { get; set; }
public int AtomicNumber { get; set; }
public int Id { get; set; }
public string toString()
{
return Symbol + " " + Name + " " + AtomicNumber + " " + Id;
}
}
class Program
{
private static void AddToDictionary(Dictionary<int, MyObj> elements,
string symbol, string name, int atomicNumber, int id)
{
MyObj theElement = new MyObj();
theElement.Symbol = symbol;
theElement.Name = name;
theElement.AtomicNumber = atomicNumber;
theElement.Id = id;
elements.Add(key: theElement.Id, value: theElement);
}
private static Dictionary<int, MyObj> BuildDictionary()
{
var elements = new Dictionary<int, MyObj>();
AddToDictionary(elements, "K", "Potassium", 19, 0);
AddToDictionary(elements, "Ca", "Calcium", 20, 1);
AddToDictionary(elements, "Sc", "Scandium", 21, 2);
AddToDictionary(elements, "Ti", "Titanium", 22, 3);
return elements;
}
static List<T> GetListOfProperty<T>(Dictionary<int, MyObj> colBlobs, string property)
{
Type t = typeof(MyObj);
PropertyInfo prop = t.GetProperty(property);
if (prop == null)
{
// throw new Exception(string.Format("Property {0} not found", f.Name.ToLower()));
Console.WriteLine(string.Format("Property {0} not found", property));
return new List<T>();
}
//still need to fix this
return colBlobs.Values
.Select(blob => (T)prop.GetValue(blob))
.OrderBy(x => x)
.ToList();
}
static SortedDictionary<int, MyObj> GetListOfProperty2<T>(Dictionary<int, MyObj> colBlobs, string property)
{
// CODE?
return sortedDict;
}
static void Main(string[] args)
{
Dictionary<int, MyObj> myColl = BuildDictionary();
var res = GetListOfProperty<string>(myColl, "Name");
foreach (var rr in res)
Console.WriteLine(rr.ToString());
//outputs : Which is only one property, the one selected
//--------
//Calcium
//Potassium
//Scandium
//Titanium
var res2 = GetListOfProperty2<string>(myColl, "Name");
//want to output the whole dictionary
//<1, {"Ca", "Calcium", 20,1}
//<0, {"K", "Potassium", 19, 0}
//<2, {"Sc", "Scandium", 21, 2}
//<3, {"Ti", "Titanium", 22, 3}
}
}
Since it seems to be that it is unclear what I want. I added example output. I am pretty sure there is no way to make this question more clear.
The problem with SortedDictionary is that it can only be sorted by Key, so you'll have to use OrderBy() in some way:
public static IOrderedEnumerable<KeyValuePair<K, T>> SortByMember<K, T>(this Dictionary<K, T> data, string memberName)
{
Type type = typeof(T);
MemberInfo info = type.GetProperty(memberName) ?? type.GetField(memberName) as MemberInfo;
Func<KeyValuePair<K, T>, object> getter = kvp => kvp.Key;
if (info is PropertyInfo pi)
getter = kvp => pi.GetValue(kvp.Value);
else if (info is FieldInfo fi)
getter = kvp => fi.GetValue(kvp.Value);
return data.OrderBy(getter);
}
This can handle both properties and fields, and if the member name is invalid, it defaults to sorting by key.
You can change that to not sort if member name is invalid by changing the return value:
public static IEnumerable<KeyValuePair<K, T>> SortByMember<K, T>(this Dictionary<K, T> data, string memberName)
{
Type type = typeof(T);
MemberInfo info = type.GetProperty(memberName) ?? type.GetField(memberName) as MemberInfo;
if (info == null) return data;
Func<KeyValuePair<K, T>, object> getter = null;
if (info is PropertyInfo pi)
getter = kvp => pi.GetValue(kvp.Value);
else if (info is FieldInfo fi)
getter = kvp => fi.GetValue(kvp.Value);
return data.OrderBy(getter);
}
IMO it is wrong to return an empty dictionary if it fails to find the member. Alternatively you can throw an exception.
Use case:
Dictionary<int, MyObj> myColl = BuildDictionary();
var res = myColl.SortByMember("Name");
foreach (var rr in res)
Console.WriteLine(rr.Value);
To achieve your goal you can use expression tree to build a compiled lambda and then use this lambda in the Linq OrderBy() method:
public static class ExpressionHelper
{
public static Func<T, object> GetMemberExpressionFunc<T>(string memberName)
{
var parameter = Expression.Parameter(typeof(T));
Expression source = Expression.PropertyOrField(parameter, memberName);
Expression conversion = Expression.Convert(source, typeof(object));
return Expression.Lambda<Func<T, object>>(conversion, parameter).Compile();
}
}
static void Main(string[] args)
{
Dictionary<int, MyObj> myColl = BuildDictionary();
string property = "AtomicNumber"; // or whatever property you want your dictionary ordered by
var func = ExpressionHelper.GetMemberExpressionFunc<MyObj>(property);
var ordered = myColl.OrderBy(x => func(x.Value));
}

How to use dynamically generated OrderBy in LINQ?

Based on API, I can have multiple parameters which can be used in order by. There is a function which creates a dynamic order by parameter as a string. I want to use this in .OrderBy but not sure how to do this.
API Call:
{{url}}?keyword=singer&page=12&size=5&sortby=LastName&sortby=FirstName
Code:
public CallCenterPageResult<CallCenterCustomerSummary> GetCustomers(int page, int pageSize, IEnumerable<SortParameter> sortParameters, string keyword)
{
using (var ctx = new EFCallCenterContext())
{
var customerDetails = ctx.CallCenterCustomers
.Where(ccs => ccs.IsDeleted == false && (ccs.FirstName.Contains(keyword) || ccs.LastName.Contains(keyword) || ccs.Phone.Contains(keyword)))
.OrderBy(sortParameters.ToOrderBy()) // "LastName ASC, FirstName ASC"
.Skip(pageSize * (page - 1)).Take(pageSize)
.ToList();
return customerDetails;
}
}
Extension Method to get order by:
static class RepositoryExtensions
{
public static string ToOrderBy(this IEnumerable<SortParameter> parameters)
{
return string.Join(", ", parameters.Select(p => p.SortBy + (p.Descending ? " DESC" : " ASC")));
}
}
Output:
"LastName ASC, FirstName ASC"
Extension method to accept dynamic LINQ:
public static class Extension
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
var props = property.Split('.');
var type = typeof(T);
var 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);
expr = Expression.Property(expr, pi); // Errors out here.
type = pi.PropertyType;
}
var delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
var lambda = Expression.Lambda(delegateType, expr, arg);
var result = 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[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
}
Error:
System.ArgumentNullException: Value cannot be null.
Parameter name: property
Scrren shot:
This is the first time working with this complex query so not sure how to do this. I can add more info if needed.
It looks like the error occurs because pi is null. And it is null because, I would assume, the class standing in for the T generic doesn't have a property named LastName ASC, FirstName ASC. I would try something like the following:
var props = property.Split(",");
... //this code stays the same
foreach(string prop in props) {
var propNameAndDirection = prop.Split(" ");
PropertyInfo pi = type.GetProperty(propNameAndDirection[0]);
... //continue as necessary, using propNameAndDirection[1]
... //to decide OrderBy or OrderByDesc call
Hopefully this sets you in the right direction.
After some trial and error, I am able to find an answer.
Tested with followings:
.OrderBy("LastName ASC, FirstName ASC")
.OrderBy("LastName ASC")
.OrderBy("LastName ASC,FirstName DESC")
Linq:
public CallCenterPageResult<CallCenterCustomerSummary> GetCustomers(int page, int pageSize, IEnumerable<SortParameter> sortParameters, string keyword)
{
using (var ctx = new EFCallCenterContext())
{
var customerDetails = ctx.CallCenterCustomers
.Where(ccs => ccs.IsDeleted == false && (ccs.FirstName.Contains(keyword) || ccs.LastName.Contains(keyword) || ccs.Phone.Contains(keyword)))
.OrderBy(o => o.Equals(sortParameters.ToOrderBy()))
.Skip(pageSize * (page - 1)).Take(pageSize)
.ToList();
return customerDetails;
}
}
Helper Class:
public static class OrderByHelper
{
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> enumerable, string orderBy)
{
return enumerable.AsQueryable().OrderBy(orderBy).AsEnumerable();
}
public static IQueryable<T> OrderBy<T>(this IQueryable<T> collection, string orderBy)
{
foreach (var orderByInfo in ParseOrderBy(orderBy))
{
collection = ApplyOrderBy(collection, orderByInfo);
}
return collection;
}
private static IQueryable<T> ApplyOrderBy<T>(IQueryable<T> collection, OrderByInfo orderByInfo)
{
var props = orderByInfo.PropertyName.Split('.');
var type = typeof (T);
var arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (var prop in props)
{
var pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
var delegateType = typeof (Func<,>).MakeGenericType(typeof (T), type);
var lambda = Expression.Lambda(delegateType, expr, arg);
string methodName;
if (!orderByInfo.Initial && collection is IOrderedQueryable<T>)
{
methodName = orderByInfo.Direction == SortDirection.Ascending ? "ThenBy" : "ThenByDescending";
}
else
{
methodName = orderByInfo.Direction == SortDirection.Ascending ? "OrderBy" : "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)
{
if (string.IsNullOrEmpty(orderBy))
{
yield break;
}
var items = orderBy.Split(',');
var initial = true;
foreach (var item in items)
{
var pair = item.Trim().Split(' ');
if (pair.Length > 2)
{
throw new ArgumentException(
$"Invalid OrderBy string '{item}'. Order By Format: Property, Property2 ASC, Property2 DESC");
}
var prop = pair[0].Trim();
if (string.IsNullOrEmpty(prop))
{
throw new ArgumentException(
"Invalid Property. Order By Format: Property, Property2 ASC, Property2 DESC");
}
var 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
}
Referances:
Dynamic LINQ OrderBy on IEnumerable<T>
http://aonnull.blogspot.com/2010/08/dynamic-sql-like-linq-orderby-extension.html
If I understood the problem correctly.
Expression<Func<TEntity, TKey>> genericParameter = null;
genericParameter = x => x.foo;
var customerDetails = ctx.CallCenterCustomers
.Where(ccs => ccs.IsDeleted == false && (ccs.FirstName.Contains(keyword) || ccs.LastName.Contains(keyword) || ccs.Phone.Contains(keyword)))
.OrderBy(genericParameter)

Can I pass through a variable class type to <T>

Below is an example of a class that, searches an item in a DataGrid for text matches in it's property values. I currently call it with:
FullTextSearchNext<UserViewModel>.FullTextSearchInit();
The <UserViewModel> hardcoded above is what the ItemsSource in my DataGrid consists of. To clarify: the collection is made up of UserViewModel items.
What I am wondering is, is there a way I can get the item class (UserViewModel) from the DataGrid and replace the hardcoded <UserViewModel> with a variable of some sort? Thus making my calls to the ClassPropertySearch generic as well as the class itself.
public static class ClassPropertySearch<T>
{
public static bool Match(T item, string searchTerm)
{
bool match = _properties.Select(prop => prop(item)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
private static List<Func<T, string>> _properties;
public static void FullTextSearchInit()
{
_properties = GetPropertyFunctions().ToList();
}
}
[EDIT]
This is to show more of the class, which I should have done initially.
Includes Marius'solution:
Now that <T> is removed from ClassPropertySearch the other functions such as GetPropertyFunctions, etc, do not work , should I just pass the type through to them as parameters?
public static class ClassPropertySearch
{
public static bool Match(Type itemType, string searchTerm)
{
bool match = _properties.Select(prop => prop(itemType)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
private static List<Func<Type, string>> _properties;
public static void FullTextSearchInit(List<string> binding_properties)
{
_properties = GetPropertyFunctions().ToList();
}
public static IEnumerable<Func<Type, string>> GetPropertyFunctions()
{
return GetStringPropertyFunctions();
}
public static IEnumerable<Func<Type, string>> GetStringPropertyFunctions()
{
var propertyInfos = typeof(Type).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty)
.Where(p => p.PropertyType == typeof(string)).ToList();
var properties = propertyInfos.Select(GetStringPropertyFunc);
return properties;
}
public static Func<Type, string> GetStringPropertyFunc(PropertyInfo propInfo)
{
ParameterExpression x = System.Linq.Expressions.Expression.Parameter(typeof(Type), "x");
Expression<Func<Type, string>> expression = System.Linq.Expressions.Expression.Lambda<Func<Type, string>>(System.Linq.Expressions.Expression.Property(x, propInfo), x);
Func<Type, string> propertyAccessor = expression.Compile();
return propertyAccessor;
}
}
You could replace the generic type T with type Type.
public static class ClassPropertySearch
{
public static bool Match(Type itemType, string searchTerm)
{
bool match = _properties.Select(prop => prop(itemType)).Any(value => value != null && value.ToLower().Contains(searchTerm.ToLower()));
return match;
}
private static List<Func<Type, string>> _properties;
public static void FullTextSearchInit()
{
_properties = GetPropertyFunctions().ToList();
}
}
So instead of passing the UserViewModel you would pass typeof(UserViewModel). Of course, since you need this at runtime, you need to say obj.GetType().

LINQ statement, select field that has specific attribute

I have a class with a few properties, I've got a custom attribute setup, one for TextField and one for ValueField, I am using an IEnumerable, so can't just select the fields I want, I need to basically:
collectionItems.ToDictionary(o => o.FieldWithAttribute<TextField>, o => o.FieldWithAttribute<ValueField>);
Hopefully you get what I am trying to do, this doesn't need to use generics as above, I just need to do something similar to get the marked fields from a large object, so I can have a nice little dictionary of key value pairs.
Example class that is the TEntity:
public class Product
{
[TextField]
public string ProductTitle { get; set; }
[ValueField]
public int StyleID { get; set; }
//Other fields...
}
Any ideas how I can achieve this? Perhaps using reflection somehow in the LINQ statement?
If you're going to use reflection, you should probably cache the member accessors to avoid the performance hit of reflecting on every item, every time. You could do something like this:
// Type aliases used for brevity
using Accessor = System.Func<object, object>;
using E = System.Linq.Expressions.Expression;
internal static class AttributeHelpers
{
private const BindingFlags DeclaredFlags = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
private const BindingFlags InheritedFlags = BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic;
private static readonly Accessor NullCallback = _ => null;
[ThreadStatic]
private static Dictionary<Type, Dictionary<Type, Accessor>> _cache;
private static Dictionary<Type, Accessor> GetCache<TAttribute>()
where TAttribute : Attribute
{
if (_cache == null)
_cache = new Dictionary<Type, Dictionary<Type, Accessor>>();
Dictionary<Type, Accessor> cache;
if (_cache.TryGetValue(typeof(TAttribute), out cache))
return cache;
cache = new Dictionary<Type, Accessor>();
_cache[typeof(TAttribute)] = cache;
return cache;
}
public static object MemberWithAttribute<TAttribute>(this object target)
where TAttribute : Attribute
{
if (target == null)
return null;
var accessor = GetAccessor<TAttribute>(target.GetType());
if (accessor != null)
return accessor(target);
return null;
}
private static Accessor GetAccessor<TAttribute>(Type targetType)
where TAttribute : Attribute
{
Accessor accessor;
var cache = GetCache<TAttribute>();
if (cache.TryGetValue(targetType, out accessor))
return accessor;
var member = FindMember<TAttribute>(targetType);
if (member == null)
{
cache[targetType] = NullCallback;
return NullCallback;
}
var targetParameter = E.Parameter(typeof(object), "target");
var accessorExpression = E.Lambda<Accessor>(
E.Convert(
E.MakeMemberAccess(
E.Convert(targetParameter, targetType),
member),
typeof(object)),
targetParameter);
accessor = accessorExpression.Compile();
cache[targetType] = accessor;
return accessor;
}
private static MemberInfo FindMember<TAttribute>(Type targetType)
where TAttribute : Attribute
{
foreach (var property in targetType.GetProperties(DeclaredFlags))
{
var attribute = property.GetCustomAttribute<TAttribute>();
if (attribute != null)
return property;
}
foreach (var field in targetType.GetFields(DeclaredFlags))
{
var attribute = field.GetCustomAttribute<TAttribute>();
if (attribute != null)
return field;
}
foreach (var property in targetType.GetProperties(InheritedFlags))
{
var attribute = property.GetCustomAttribute<TAttribute>();
if (attribute != null)
return property;
}
foreach (var field in targetType.GetFields(InheritedFlags))
{
var attribute = field.GetCustomAttribute<TAttribute>();
if (attribute != null)
return field;
}
return null;
}
}
It's up to you how you want to deal with items whose types lack the desired attributed members. I chose to return null.
Example usage:
var lookup = Enumerable
.Range(1, 20)
.Select(i => new Product { Title = "Product " + i, StyleID = i })
.Select(
o => new
{
Text = o.MemberWithAttribute<TextFieldAttribute>(),
Value = o.MemberWithAttribute<ValueFieldAttribute>()
})
.Where(o => o.Text != null && o.Value != null)
.ToDictionary(o => o.Text, o => o.Value);
foreach (var key in lookup.Keys)
Console.WriteLine("{0}: {1}", key, lookup[key]);
public static object FieldWithAttribute<T>(this object obj)
{
var field = obj.GetType()
.GetProperties()
.SIngleOrDefault(x => x.CustomAattributes.Any(y => y.AttributeType == typeof(T));
return field != null ? field.GetValue(obj) : null;
}
something like this
This should do the trick
public static TRet FieldWithAttribute<TAttr, TRet>(this object obj) where TAttr : Attribute
{
var field = obj.GetType()
.GetProperties()
.SingleOrDefault(x => Attribute.IsDefined(x, typeof (TAttr)));
return field == null ? default(TRet) : (TRet)field.GetValue(obj);
}
and when you use it
var dictionary = products.ToDictionary(x => x.FieldWithAttribute<TextFieldAttribute, string>(),
x => x.FieldWithAttribute<ValueFieldAttribute, int>());

Dynamic LINQ OrderBy + Method Count

I'm displaying a table of objects Company in a webpage and I am using a Dynamic Linq OrderBy to sort them on each property.
I'm using this code https://stackoverflow.com/a/233505/265122
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName) {
string[] props = property.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);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = 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[] {source, lambda});
return (IOrderedQueryable<T>)result;
}
It's great but I would also like to sort the companies on the number of employees.
Like this : query.OrderBy("Employees.Count")
I tried to call to the Count method dynamically without any success so far.
I modified the code like this :
foreach(string prop in props)
{
if (prop == "Count")
{
var countMethod = (typeof(Enumerable)).GetMethods().First(m => m.Name == "Count").MakeGenericMethod(type);
expr = Expression.Call(countMethod, expr);
break;
}
// Use reflection (not ComponentModel) to mirror LINQ.
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
But I have an exception on the expr = Expression.Call(countMethod, expr);
The exception is:
ArgumentException
Expression of type 'System.Collections.Generic.ICollection`1[Employee]'
cannot be used for parameter of type
'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
[Employee]]' of method 'Int32 Count[ICollection`1]
System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
Employee]])'
Any idea on how to achieve that?
From your gist below I have found a easy way t flatten the properties across all base types and interfaces as demonstrated on this post.
So I implemented the extension method for PropertyInfo that will return all properties from all interfaces and base classes inherited by the type. The problem was that IList does not have a Count property however iCollection does. The public static PropertyInfo[] GetPublicProperties(this Type type) will flat all properties and we get the correct one from there this should work on any property now not only Count.
public class Program
{
private static IList<Company> _companies;
static void Main(string[] args)
{
var sort = "Employees.Count";
_companies = new List<Company>();
_companies.Add(new Company
{
Name = "c2",
Address = new Address {PostalCode = "456"},
Employees = new List<Employee> {new Employee(), new Employee()}
});
_companies.Add(new Company
{
Name = "c1",
Address = new Address {PostalCode = "123"},
Employees = new List<Employee> { new Employee(), new Employee(), new Employee() }
});
//display companies
_companies.AsQueryable().OrderBy(sort).ToList().ForEach(c => Console.WriteLine(c.Name));
Console.ReadLine();
}
}
public class Company
{
public string Name { get; set; }
public Address Address { get; set; }
public IList<Employee> Employees { get; set; }
}
public class Employee{}
public class Address
{
public string PostalCode { get; set; }
}
public static class OrderByString
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.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.GetPublicProperties().FirstOrDefault(c => c.Name == prop);
if (pi != null)
{
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
else { throw new ArgumentNullException(); }
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = 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[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
public static PropertyInfo[] GetPublicProperties(this Type type)
{
if (type.IsInterface)
{
var propertyInfos = new List<PropertyInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetProperties(
BindingFlags.FlattenHierarchy
| BindingFlags.Public
| BindingFlags.Instance);
var newPropertyInfos = typeProperties
.Where(x => !propertyInfos.Contains(x));
propertyInfos.InsertRange(0, newPropertyInfos);
}
return propertyInfos.ToArray();
}
return type.GetProperties(BindingFlags.FlattenHierarchy
| BindingFlags.Public | BindingFlags.Instance);
}
}

Categories

Resources