简体   繁体   English

Lambda表达式中的C#字符串串联

[英]C# string concatenation in Lambda expression

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. 我试图将我的查询字符串输出附加到Lambda表达式中的位置。 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. 如何将其连接起来并使lambda表达式起作用。

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);

        }

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. 您将需要Where子句的类型,我将假设您的类型是Project。

If you are using linq to sql you will need to construct an Expression 如果您使用linq到sql,则需要构造一个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 如果对对象使用linq,则可以省略表达式

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

Now you can use this in your where clause 现在您可以在where子句中使用它

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. 最好的选择是传递“ Test”和状态的整数,然后将其放入表达式中。

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;

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 http://jp-labs.blogspot.com/2008/11/dynamic-lambda-expressions-using.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM