Dynamic linq order by on nested property with null properties - c#

I'm using this dynamic linq orderby function which I got from here.
This works fine with nested properties so I could do this:
var result = data.OrderBy("SomeProperty.NestedProperty");
The problem is that if SomeProperty is null then performing the OrderBy on the NestedProperty throws the infamous "Object reference not set to an instance of an object".
My guess is that I need to customize the following lines to handle the exception:
expr = Expression.Property(expr, pi);
// Or
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
I thought about creating a statement body where I could in the worst case scenario use a try catch but that didn't work as you can't have statement bodies within orderby linq statements: "A lambda expression with a statement body cannot be converted to an expression tree"
I'm lost over here, any suggestions on how I can accomplish this?
By the way, this is for Linq to Objects, not database related.

static void Main(string[] args)
{
var data = new List<MyType>() {
new MyType() { SomeProperty = new Inner() { NestedProperty = "2" }},
new MyType() { SomeProperty = new Inner() { NestedProperty = "1" }},
new MyType() { SomeProperty = new Inner() { NestedProperty = "3" }},
new MyType(),
}.AsQueryable();
var sorted = data.OrderBy(x => GetPropertyValue(x, "SomeProperty.NestedProperty"));
foreach (var myType in sorted)
{
try
{
Console.WriteLine(myType.SomeProperty.NestedProperty);
}
catch (Exception e)
{
Console.WriteLine("Null");
}
}
}
public static object GetPropertyValue(object obj, string propertyName)
{
try
{
foreach (var prop in propertyName.Split('.').Select(s => obj.GetType().GetProperty(s)))
{
obj = prop.GetValue(obj, null);
}
return obj;
}
catch (NullReferenceException)
{
return null;
}
}

How about generics:
Helper Method:
public static Expression<Func<TEntity, TResult>> GetExpression<TEntity, TResult>(string prop)
{
var param = Expression.Parameter(typeof(TEntity), "p");
var parts = prop.Split('.');
Expression parent = parts.Aggregate<string, Expression>(param, Expression.Property);
Expression conversion = Expression.Convert(parent, typeof (object));
var tryExpression = Expression.TryCatch(Expression.Block(typeof(object), conversion),
Expression.Catch(typeof(object), Expression.Constant(null)));
return Expression.Lambda<Func<TEntity, TResult>>(tryExpression, param);
}
Sample Hierarchy:
public class A
{
public A(B b)
{
B = b;
}
public B B { get; set; }
}
public class B
{
public B(C c)
{
C = c;
}
public C C { get; set; }
}
public class C
{
public C(int id)
{
this.Id = id;
}
public int Id { get; set; }
}
Example:
var list = new List<B>
{
new B(new A(new C(1))),
new B(new A(new C(2))),
new B(new A(new C(3))),
new B(new A(null)),
new B(null)
}.AsQueryable();
var ordered = list.OrderByDescending(GetExpression<B, Object>("AProp.CProp.Id"));
Output:
3
2
1
Null
Null

Related

C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition

I have two tables called ShipmentType and Books. Entity class has been mapped for these tables. Created another class called BookShipment which contains two properties, classes of ShipmentType and Book.
public class BookShipment
{
public ShipmentType Shipment { get; set; }
public Books Book { get; set; }
}
I was trying to create a where expression as follows.
Expression<Func<BookShipment, bool>> expr = x => (x.Shipment.ID == 1 && x.Book.ID == 1);
var result = from c in styp
join d in book
on c.ID equals d.ID
select new BookShipment { Shipment = c, Book = d };
var List = result.Where(expr).ToList();
and above expression at where clause is working fine and getting the result from database.
Tried to create a dynamic expression same as above expr expression but it is giving error.
BookShipment table = new BookShipment();
table.Shipment = new ShipmentType();
table.Book = new Books();
ParameterExpression ParameterL = Expression.Parameter(table.GetType(), "x");
ParameterExpression Parameter1 = Expression.Parameter(table.Shipment.GetType(), "x.Shipment");
ParameterExpression Parameter2 = Expression.Parameter(table.Book.GetType(), "x.Book");
var Property1 = Expression.Property(Parameter1, "ID");
var Property2 = Expression.Property(Parameter2, "ID");
var Clause1 = Expression.Equal(Property1, Expression.Constant(1));
var Clause2 = Expression.Equal(Property2, Expression.Constant(1));
var Lambda1 = Expression.Lambda<Func<ShipmentType, bool>>(Clause1, Parameter1);
var Lambda2 = Expression.Lambda<Func<Books, bool>>(Clause2, Parameter2);
var OrElseClause = Expression.Or(Lambda1.Body, Lambda2.Body);
var Lambda = Expression.Lambda<Func<BookShipment, bool>>(OrElseClause, ParameterL);
var result = from c in styp
join d in book
on c.ID equals d.ID
select new BookShipment { Shipment = c, Book = d };
var record = result.Where(Lambda).ToList();
When executing above Where clause it is giving error.
{System.InvalidOperationException: The LINQ expression 'DbSet<ShipmentType>
.Join(
outer: DbSet<Books>,
inner: s => s.ID,
outerKeySelector: b => b.BookID,
innerKeySelector: (s, b) => new TransparentIdentifier<ShipmentType, Books>(
Outer = s,
Inner = b
))
.Where(ti => ti.Shipment.ID == 1 || ti.Book.BookID == 1)' could not be translated.
when you create an expression to pass into the LINQ where function, take the following hints:
the type is Expression<Func<T,Bool>> ...
that means you have ONE parameter of type T, and you return a bool
if you create two Lambdas with this type, and you want to combine them... even though you have the same type T as parameter, the two parameter instances are not the same ... you will have to traverse the tree and replace the parameter so there is only one instance...
if you'd like an example code ... here you go...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace SoExamples.ExpressionTrees
{
class Program
{
static void Main(string[] args)
{
var expr1 = GetExpression<Func<A, bool>>(x => x.Prop1 == 42);
var expr2 = GetExpression<Func<A, bool>>(x => x.Prop2 == "foo");
var expr3 = GetConstComparison<A, int>("Prop3.Prop1", 123);
var test = new A { Prop1 = 42, Prop2 = "foo", Prop3 = new B { Prop1 = 123 } };
var f1 = expr1.Compile();
var t1 = f1(test);
var f2 = expr2.Compile();
var t2 = f2(test);
var f3 = expr3.Compile();
var t3 = f3(test);
Expression tmp = Expression.AndAlso(Expression.AndAlso(expr1.Body, expr2.Body), expr3.Body);
tmp = new ParamReplaceVisitor(expr2.Parameters.First(), expr1.Parameters.First()).Visit(tmp);
tmp = new ParamReplaceVisitor(expr3.Parameters.First(), expr1.Parameters.First()).Visit(tmp);
var expr4 = Expression.Lambda<Func<A, bool>>(tmp, expr1.Parameters.First());
var f4 = expr4.Compile();
var t4 = f4(test);
var list = new List<A> { test };
var result = list.AsQueryable().Where(expr4).ToList();
}
static Expression<TDelegate> GetExpression<TDelegate>(Expression<TDelegate> expr)
{
return expr;
}
static Expression<Func<T, bool>> GetConstComparison<T, P>(string propertyNameOrPath, P value)
{
ParameterExpression paramT = Expression.Parameter(typeof(T), "x");
Expression expr = getPropertyPathExpression(paramT, propertyNameOrPath.Split('.'));
return Expression.Lambda<Func<T, bool>>(Expression.Equal(expr, Expression.Constant(value)), paramT);
}
private static Expression getPropertyPathExpression(Expression expr, IEnumerable<string> propertyNameOrPath)
{
var mExpr = Expression.PropertyOrField(expr, propertyNameOrPath.First());
if (propertyNameOrPath.Count() > 1)
{
return getPropertyPathExpression(mExpr, propertyNameOrPath.Skip(1));
}
else
{
return mExpr;
}
}
}
public class ParamReplaceVisitor : ExpressionVisitor
{
private ParameterExpression orig;
private ParameterExpression replaceWith;
public ParamReplaceVisitor(ParameterExpression orig, ParameterExpression replaceWith)
{
this.orig = orig;
this.replaceWith = replaceWith;
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (node == orig)
return replaceWith;
return base.VisitParameter(node);
}
}
public class A
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public B Prop3 { get; set; }
}
public class B
{
public int Prop1 { get; set; }
}
}
of course you will want to add errorhandling etc...

Building a dynamic expression tree to filter on a collection property 2

Maybe this is a duplicate thread, but I am going to try, because there is a tiny difference.
I am trying to build a dynamic expression to filter a collection property.
The code:
public class TestEntity
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<string> Values { get; set; }
}
public class TestFilter
{
public TestFilter()
{
var itens = new List<TestEntity>();
itens.Add(new TestEntity { ID = 1, Name = "Test1", Values = new List<string> { "V1", "V2" } });
itens.Add(new TestEntity { ID = 2, Name = "Test2", Values = new List<string> { "V6", "V3" } });
itens.Add(new TestEntity { ID = 3, Name = "Test3", Values = new List<string> { "V4", "V5" } });
itens.Add(new TestEntity { ID = 4, Name = "Test4", Values = new List<string> { "V2", "V3" } });
itens = itens.Where(e => e.Values.Any(c => c.Equals("V2"))).ToList();
**//Result IDs: 1, 4**
}
}
The filter above will give me IDs 1 and 4 as result.
I want to filter entities where exists a certain value in the collection "Values".
So far, I have tried this thread, but didnt realize how it can be done.
Any help would be apreciated.
So you are seeking for a method which given a collection property name and value will produce Where predicate like e => e.Collection.Any(c => c == value).
You can use the following extension method (hope the code is self explanatory):
public static class QueryableExtensions
{
public static IQueryable<T> WhereAnyEquals<T>(this IQueryable<T> source, string collectionName, object value)
{
var e = Expression.Parameter(typeof(T), "e");
var collection = Expression.PropertyOrField(e, collectionName);
var itemType = (collection.Type.IsIEnumerableT() ? collection.Type :
collection.Type.GetInterfaces().Single(IsIEnumerableT))
.GetGenericArguments()[0];
var c = Expression.Parameter(itemType, "c");
var itemPredicate = Expression.Lambda(
Expression.Equal(c, Expression.Constant(value)),
c);
var callAny = Expression.Call(
typeof(Enumerable), "Any", new Type[] { itemType },
collection, itemPredicate);
var predicate = Expression.Lambda<Func<T, bool>>(callAny, e);
return source.Where(predicate);
}
private static bool IsIEnumerableT(this Type type)
{
return type.IsInterface && type.IsConstructedGenericType &&
type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}
}
like this:
itens = itens.AsQueryable().WhereAnyEquals("Values", "V2").ToList();
If you step through the code, the variable predicate contains the expression you are asking for.

Get requested column distinct values from list

I have a list with multiple columns. I want to filter the list based on the requested column name (column name will come as a parameter) with distinct values.
IList<obj1> objTemp= new List<obj1>();
for (int i = 0; i < 15; i++)
{
obj1 temp= new obj1();
temp.Name = "Name" + i;
temp.Age= "Age" + i;
temp.Company= "Company" + i;
objTemp.Add(temp);
}
var distinctTypeIDs = objTemp.Select(x => x.**{my requested column}**).Distinct();
You can use reflection for getting desired property by it's name:
var distinctTypeIDs = objTemp.Select(x => x.GetType().GetProperty("requested_column").GetValue(x))
.Distinct();
I've always been a fan of "mapping" a column to an anonymous method responsible for retrieving the contents of that column:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var items = new List<SomeObject> {new SomeObject { Age = 10, Name = "Daniel", Company = "InCycle" },
{new SomeObject { Age = 20, Name = "Not Daniel", Company = "Not InCycle" }
}};
var result = Filter<int>(items, "Age");
Console.WriteLine(result.Last());
}
public static IEnumerable<T> Filter<T>(IEnumerable<SomeObject> items, string filterCriteria)
{
var mappings = new Dictionary<string, Func<IEnumerable<SomeObject>, IEnumerable<T>>>
{
{ "Age", filterItems => filterItems.Select(item => item.Age).Distinct().Cast<T>() },
{ "Name", filterItems => filterItems.Select(item => item.Name).Distinct().Cast<T>() },
{ "Company", filterItems => filterItems.Select(item => item.Company).Distinct().Cast<T>() }
};
return mappings[filterCriteria](items);
}
}
public class SomeObject
{
public int Age {get;set;}
public string Name {get;set;}
public string Company {get; set;}
}
The downside to this approach is that if you add additional properties, you could forget to add them to the filtering. Expressions are a solid approach as well.
One way is to use a method like this.
private IList<obj1> SortListAccordingToParameter(string filter)
{
if(filter == "Name")
return objTemp.Select(x => x.Name).Distinct();
else if(filter == "Age")
return objTemp.Select(x => x.Age).Distinct();
else if(filter == "Company")
return objTemp.Select(x => x.Company).Distinct();
}
If you know the type of the property you will be searching for, you could use expressions.
string propName = "Age";
var paramExpression = Expression.Parameter(typeof(Obj1));
// o =>
var memberExpression = Expression.Property(paramExpression, propName);
// o => o.Age
var lambdaExpression = Expression.Lambda<Func<Obj1, string>>(memberExpression, paramExpression);
// (o => o.Age)
var compiled = lambdaExpression.Compile();
IList<Obj1> objTemp = new List<Obj1>();
for (var i = 0; i < 15; i++) {
Obj1 temp = new Obj1();
temp.Name = "Name" + i;
temp.Age = "Age" + i;
temp.Company = "Company" + i;
objTemp.Add(temp);
}
var results = objTemp.Select(compiled);
// equivalent to objTemp.Select(o => o.Age), plus a delegate call and the time to
// compile the lambda.
I would probably wrap this up in a static class, like this:
static class Gen<TModel, TProp> {
public static Func<TModel, TProp> SelectorExpr(string propertyName) {
var pExpr = Expression.Parameter(typeof (TModel));
var mExpr = Expression.Property(pExpr, propertyName);
var lExpr = Expression.Lambda<Func<TModel, TProp>>(mExpr, pExpr);
return lExpr.Compile();
}
}
that way you can write your selector like:
var results = objTemp.Select(Gen<Obj1, string>.SelectorExpr(propName));
That seems a bit more clear to me what it is I'm doing, especially if I'm reading expression DOM code I wrote 6 months after.
public class Test
{
public string name { get; set; }
public string age { get; set; }
public string contact { get; set; }
public Test getName(string name)
{
List<Test> testList = new List<Test>();
testList.Add(new Test { name = "Developer", age = "24", contact = "99009900990" });
testList.Add(new Test { name = "Tester", age = "30", contact = "009900990099" });
return testList.Where(c => c.name == name).FirstOrDefault();
}
}
static void Main(string[] args)
{
Test testObj = new Test();
Test selectedObj = testObj.getName("Developer");
}

build an expression with multiple sorting

I am trying to build an expression for sorting, and i wrote code that sorts my list using one property.
But I need to sort it firstly by one property, secondly by another property and so on.
I mean I want to build an expression that will implement something like that: students.OrderBy(fistExpression.Compile()).ThenBy(secondImpression.Complie()).ThenBy(thirdExpression.Compile()).
So how to dynamically put that ThenBy methods?
Here is my code:
Type studentType = typeof(Student);
ParameterExpression studentParam = Expression.Parameter(studentType, "x");
MemberInfo ageProperty = studentType.GetProperty("Age");
MemberExpression valueInNameProperty =
Expression.MakeMemberAccess(studentParam, ageProperty);
Expression<Func<Student, int>> orderByExpression =
Expression<Func<Student, int>>.Lambda<Func<Student, int>>(valueInNameProperty, studentParam);
var sortedStudents = students.OrderBy(orderByExpression.Compile());
My solution:
public static Func<Student, object> BuildPredicate(string propertyName)
{
Type studentType = typeof(Student);
ParameterExpression studentParam = Expression.Parameter(studentType, "x");
MemberInfo ageProperty = studentType.GetProperty(propertyName);
MemberExpression valueInNameProperty = Expression.MakeMemberAccess(studentParam, ageProperty);
UnaryExpression expression = Expression.Convert(valueInNameProperty, typeof (object));
Expression<Func<Student, object>> orderByExpression = Expression.Lambda<Func<Student, object>>(expression, studentParam);
return orderByExpression.Compile();
}
in your expression making code is added casting to object.
That is how you can create a chain of ThenBy:
var sortedStudents = students.OrderBy(BuildPredicate("Age"));
foreach (var property in typeof(Student).GetProperties().Where(x => !String.Equals(x.Name, "Age")))
{
sortedStudents = sortedStudents.ThenBy(BuildPredicate(property.Name));
}
var result = sortedStudents.ToList();
Finally, Student sample class:
public class Student
{
public int Age { get; set; }
public string Name { get; set; }
}
Update:
Another approach is using attributes to mark properies from your Student to use them in OrderBy and ThenBy. Like:
public class Student
{
[UseInOrderBy]
public int Age { get; set; }
[UseInOrderBy(Order = 1)]
public string Name { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
internal class UseInOrderByAttribute : Attribute
{
public int Order { get; set; }
}
That is how you can build sorting chain using UseInOrderByAttribute:
Type studentType = typeof (Student);
var properties = studentType.GetProperties()
.Select(x => new { Property = x, OrderAttribute = x.GetCustomAttribute<UseInOrderByAttribute>() })
.Where(x => x.OrderAttribute != null)
.OrderBy(x => x.OrderAttribute.Order);
var orderByProperty = properties.FirstOrDefault(x => x.OrderAttribute.Order == 0);
if (orderByProperty == null)
throw new Exception("");
var sortedStudents = students.OrderBy(BuildPredicate(orderByProperty.Property.Name));
foreach (var property in properties.Where(x => x.Property.Name != orderByProperty.Property.Name))
{
sortedStudents = sortedStudents.ThenBy(BuildPredicate(property.Property.Name));
}
var result = sortedStudents.ToList();
Fix: BuildPredicate can be writen without dynamic. BuildPredicate sample code is changed.
I assume that you have private properties that you want to be able to sort.
If you for example have this class:
public class Student
{
public Student (int age, string name)
{
Age = age;
Name = name;
}
private string Name { get; set; }
public int Age { get; set; }
public override string ToString ()
{
return string.Format ("[Student: Age={0}, Name={1}]", Age, Name);
}
}
You can use the following method to build expressions that will get both public and private properties:
public static Func<TType, TResult> CreateExpression<TType, TResult>(string propertyName)
{
Type type = typeof(TType);
ParameterExpression parameterExpression = Expression.Parameter(type, propertyName);
MemberInfo property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
MemberExpression valueInProperty = Expression.MakeMemberAccess(parameterExpression, property);
return Expression.Lambda<Func<TType,TResult>>(valueInProperty, parameterExpression).Compile();
}
Example of usage:
var students = new [] {
new Student(20, "Ben"),
new Student(20, "Ceasar"),
new Student(20, "Adam"),
new Student(21, "Adam"),
};
var sortedStudents = students
.OrderBy(CreateExpression<Student, string>("Name"))
.ThenBy(CreateExpression<Student, int>("Age"));
sortedStudents.ToList().ForEach(student => Console.WriteLine(student));
/*
Prints:
[Student: Age=20, Name=Adam]
[Student: Age=21, Name=Adam]
[Student: Age=20, Name=Ben]
[Student: Age=20, Name=Ceasar]
*/

How to filter collection by any property with reflection?

I have IEnumerable collection. I want to create such method:
public IEnumerable<object> Try_Filter(IEnumerable<object> collection, string property_name, string value)
{
//If object has property with name property_name,
// return collection.Where(c => c.Property_name == value)
}
Is it possible? I'm using C# 4.0.
Thanks!
Try this:
public IEnumerable<object> Try_Filter(IEnumerable<object> collection,
string property_name, string value)
{
var objTypeDictionary = new Dictionary<Type, PropertyInfo>();
var predicateFunc = new Func<Object, String, String, bool>((obj, propName, propValue) => {
var objType = obj.GetType();
PropertyInfo property = null;
if(!objTypeDictionary.ContainsKey(objType))
{
property = objType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(prop => prop.Name == propName);
objTypeDictionary[objType] = property;
} else {
property = objTypeDictionary[objType];
}
if(property != null && property.GetValue(obj, null).ToString() == propValue)
return true;
return false;
});
return collection.Where(obj => predicateFunc(obj, property_name, value));
}
Tested with:
class a
{
public string t { get; set;}
}
var lst = new List<Object> { new a() { t = "Hello" }, new a() { t = "HeTherello" }, new a() { t = "Hello" } };
var result = Try_Filter(lst, "t", "Hello");
result.Dump();
Although, this will be very slow for large collections

Categories

Resources