5
String qry = "p => p.Title == "Test" && P.Status=0;

myProjects = dataAccessHelper.GetMyDetails(id).
  Where(Append qry string here).ToList();

I am trying to append my query string output to where in Lambda expression. It works fine if I take

myProjects = dataAccessHelper.GetMyDetails(id).
  Where(p => p.Title == "Test" && P.Status=0).ToList(); 

But not like this

myProjects = dataAccessHelper.GetMyDetails(id).
  Where("+ qry +").ToList();

How can I concatenate this and make lambda expression work.

My Controller code

   [HttpGet]
        public virtual ActionResult MyProjects(int? id, string qry)
        {
            var dataAccessHelper = new DataAccessHelper(true);
            IList<ProjectEntity> myProjects;
            if (qry == null)
            {
                myProjects = dataAccessHelper.GetMyProjects(id);
            }
            else
            {
                Expression<Func<ProjectEntity, bool>> expr = qry;
                myProjects = dataAccessHelper.GetMyProjects(id).Where(expr).ToList();

            }

            var myProjectsViewModel = new MyProjectsViewModel() { GetMyProjects =                  myProjects };
            return View(myProjectsViewModel);

        }

2 Answers 2

4

What you are looking for is a Function Expression. You will need the type for the Where clause, I am going to assume that your type is Project.

If you are using linq to sql you will need to construct an Expression

//assuming Project is type
Expression<Func<Project, bool>> expr = p => p.Title == "Test" && p.Status == 0;

If you are using linq to objects then you can omit the Expression

//assuming Project is type
Func<Project, bool> expr = p => p.Title == "Test" && p.Status == 0;

Now you can use this in your where clause

myProjects = dataAccessHelper.GetMyDetails(id).Where(expr).ToList();

edit

With regards to your edit. You can't really pass in a string to do this with. Your best bet would be to pass in the "Test" and the integer for status, and then put those into the expression.

public virtual ActionResult MyProjects(int? id, string title = "Title", int status = 0)

and then

Func<Project, bool> expr = p => p.Title == title && p.Status == status;
Sign up to request clarification or add additional context in comments.

6 Comments

+1.btw. You can skip Expression<> in variable declaration if it's LINQ to Objects query.
@MarcinJuraszek - I added in the distinction just in case :)
this Expression seems to be completely new to me. My code shows cannot resolve symbol expression, details, expr. kindly let me know if I m missing any name space
@Usham - Yes, the Expression will require a namespace inclusion. using System.Linq.Expressions;
I updated my code with controller code where I am unable to get expression and TYPE mainly. Please suggest on code. Sorry for inconvenience.
|
1

For converting strings to expressions, you will need to do some form of dynamic compilation. This blog post contains some options for doing this:

http://jp-labs.blogspot.com/2008/11/dynamic-lambda-expressions-using.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.