4

I am building dynamic linq expressions which is working fine for a single entity. For example: I have a class called Employee and empeduinfo

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class EmpEduInfo
{
    public int Id { get; set; }
    public string Name  { get; set; }
    public int EmpId { get; set; }
}

I need to get all the the employees and empeduinfo class starts with "x"

I prepared expression for startswith("x")

var temp= entities.employees.Include("EmpEduInfo").Where(mydynamicexpression);

In this case it is filtering only parent table not on child.

I need to prepare generic expression so than i need to filter both parent and child objects dynamically.

Without using expression I know a solution:

var temp= (from ee in entities.Employee.Include("EmpEduInfo").Where(x => x.name.StartsWith("t"))                           
           where ee.EmpEduInfo.Where(x => x.name.StartsWith("t")).Count()>0                                
           select ee).ToList();

using expressions I am building generic expression to provide dynamic advance search rather than writing in each and every entity.

Here is my expression details

            // Get the method information for the String.StartsWith() method                
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            // Build the parameter for the expression
            ParameterExpression  empparam= Expression.Parameter(typeof(employee), "ename");;
            // Build the member that was specified for the expression
            MemberExpression field = Expression.PropertyOrField(empparam, "name");
            // Call the String.StartsWith() method on the member
            MethodCallExpression startsWith = Expression.Call(field, mi, Expression.Constant("t"));                  
            var namelamda = Expression.Lambda<Func<employee, bool>>(startsWith, new ParameterExpression[] { empparam });
            var temp = entities.employees.Include("empedudetails").Where(namelamda).ToList();
  • You probably forgot the `EmpEduInfo` collection in the `Employee` class. With 'generic expression' you mean an expression that can be applied to any class? Could you show the code of the expression? – Gert Arnold Jan 10 '13 at 08:28
  • Thanks for reply. My expression in add in above code at the botton and i included teh empeduinfo in Include pelase check the above code – user1374303 Jan 10 '13 at 09:03
  • Thanks for reply. My expression in add in above code at the botton and i included teh empeduinfo in Include pelase check the above code.for example i want to search all entities name which starts with x. example emplees are Tony,greg and edudetails are Tenth and Seventh now want to get with "t" means i need to get tony and Tength. – user1374303 Jan 10 '13 at 09:13

2 Answers2

2

You can look at the Expression the compiler generates using IQueryable:

IQueryable<Employee> query = 
  from ee in entities.Employee ...

var expression = query.Expression;

Look at expression in a debugger to see what you need to generate - LINQPad is good for this.


You might want to simplify your query a bit first:

IQueryable<Employee> query = 
  from ee in entities.Employee.Include("EmpEduInfo")                           
  where
    ee.name.StartsWith("t") &&
    ee.EmpEduInfo.Any(x => x.name.StartsWith("t"))                             
  select ee;
Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Tested with this query IQueryable query = from ee in entities.Employee.Include("EmpEduInfo") where ee.name.StartsWith("t") && ee.EmpEduInfo.Any(x => x.name.StartsWith("t")) select ee; still it is not filtering the data of child objects. – user1374303 Jan 10 '13 at 11:01
  • Do you mean you want to fetch all `Employee`s who's name starts with "T" and include only those related `EmpEduInfo` records that start with "T"? That is a different issue to generating an `Expression` in code. In EF, you have to use a `Select` clause to do that. I suggest you start a new question for that issue. – Nick Butler Jan 10 '13 at 11:39
  • Or google "ef filter navigation property" => http://stackoverflow.com/questions/11228463/load-navigation-properties-with-filter-for-entity-framework-4-3 – Nick Butler Jan 10 '13 at 11:41
  • Or my answer here: http://stackoverflow.com/questions/13915148/explicit-loading-of-child-navigation-properties-with-criteria/13916407#13916407 – Nick Butler Jan 10 '13 at 11:43
0

I was trying in EF 4.0 either we have write DB extentions for the same.

Option is provided in EF 4.1

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

Thanks.