Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用字符串动态构建Lambda表达式_C#_Asp.net Mvc_Linq_Asp.net Core_Lambda - Fatal编程技术网

C# 如何使用字符串动态构建Lambda表达式

C# 如何使用字符串动态构建Lambda表达式,c#,asp.net-mvc,linq,asp.net-core,lambda,C#,Asp.net Mvc,Linq,Asp.net Core,Lambda,这是我的密码 [HttpPost] public ActionResult TopProjects(FormCollection form) { bool top, desgin, implement; string str = ""; if (string.IsNullOrEmpty(form["top"])) { top = false; } else { top = true;str += "x=>x.IsTopProject==top"; } if (string.

这是我的密码

[HttpPost]
public ActionResult TopProjects(FormCollection form)
{
  bool top, desgin, implement;
  string str = "";
  if (string.IsNullOrEmpty(form["top"])) { top = false; } else { top = true;str += "x=>x.IsTopProject==top"; }
  if (string.IsNullOrEmpty(form["desgin"])) { desgin = false; } else { desgin = true; str += "&& x.IsAnDesigning==desgin"; }
  if (string.IsNullOrEmpty(form["implement"])) { implement = false; } else { implement = true;str += "&& x.IsAnImplementation==implement"; }

   return View(blProject.Select().Where(str));
 }
lambda从str获取错误。
我如何解决它呢?

显然
blProject
一个类的对象,它实现了某个类的IQueryable,比如说它实现了
IQueryable

你很难定义三个布尔值,你的意思是:

bool top =       String.IsNullOrEmpty(form["top"]));
bool design =    String.IsNullOrEmpty(form["desgin"]);
bool implement = String.IsNullOrEmpty(form["implement"]);
根据top/design/implement的值,这是您想要的

 Top   Design   Implement     All BlProjectItems where
  0      0         0          !IsTopProject && !IsAnDesigning && !IsAnImplementation
  0      0         1          !IsTopProject && !IsAnDesigning &&  IsAnImplementation
  0      1         0          !IsTopProject &&  IsAnDesigning && !IsAnImplementation
  0      1         1          !IsTopProject &&  IsAnDesigning &&  IsAnImplementation

  1      0         0           IsTopProject && !IsAnDesigning && !IsAnImplementation
  1      0         1           IsTopProject && !IsAnDesigning &&  IsAnImplementation
  1      1         0           IsTopProject &&  IsAnDesigning && !IsAnImplementation
  0      1         1           IsTopProject &&  IsAnDesigning &&  IsAnImplementation
换句话说,你想要:

 .Where(item => item.IsTopProject == top
             && item.IsAnDesigning == design
             && item.IsAnImplementation == implement)
您的功能如下所示:

public ActionResult TopProjects(FormCollection form)
{
    bool top =       String.IsNullOrEmpty(form["top"]));
    bool design =    String.IsNullOrEmpty(form["desgin"]);
    bool implement = String.IsNullOrEmpty(form["implement"]);

    var query = blProject.Where(item => item.IsTopProject == top
             && item.IsAnDesigning == design
             && item.IsAnImplementation == implement)

    // only if you don't want all properties of BlProjectItem:
    .Select(item => new
    {
         // Select the items that you want, e.g.
         Id = item.Id,
         Name = item.Name,
         ...
    });

    return view(Query);
}

其中,方法不将字符串作为参数。您需要提供Func谓词,请看我可以问一下为什么需要字符串吗?(top、design、implement)是树复选框。我尝试通过选中其中任何一个来筛选表。所以我需要动态lambda。
 .Where(item => item.IsTopProject == top
             && item.IsAnDesigning == design
             && item.IsAnImplementation == implement)
public ActionResult TopProjects(FormCollection form)
{
    bool top =       String.IsNullOrEmpty(form["top"]));
    bool design =    String.IsNullOrEmpty(form["desgin"]);
    bool implement = String.IsNullOrEmpty(form["implement"]);

    var query = blProject.Where(item => item.IsTopProject == top
             && item.IsAnDesigning == design
             && item.IsAnImplementation == implement)

    // only if you don't want all properties of BlProjectItem:
    .Select(item => new
    {
         // Select the items that you want, e.g.
         Id = item.Id,
         Name = item.Name,
         ...
    });

    return view(Query);
}