Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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# 4.0 具有线性实体(c#)的动力场_C# 4.0_Entity Framework 4_Linq To Entities - Fatal编程技术网

C# 4.0 具有线性实体(c#)的动力场

C# 4.0 具有线性实体(c#)的动力场,c#-4.0,entity-framework-4,linq-to-entities,C# 4.0,Entity Framework 4,Linq To Entities,我的代码: public List<tblBook> GetBook(string NameField, string Value) { return (this.Entities.Book.Where( "it.@p0 NOT LIKE @p1", new ObjectParameter("p0", string.Format("%{0}%", NameField)), new ObjectPara

我的代码:

        public List<tblBook> GetBook(string NameField, string Value)
        {
            return (this.Entities.Book.Where(
            "it.@p0 NOT LIKE @p1",

new ObjectParameter("p0", string.Format("%{0}%", NameField)),

new ObjectParameter("p1", string.Format("%{0}%", Value)))).ToList();

        }
公共列表GetBook(字符串名称字段,字符串值)
{
返回(this.Entities.Book.Where(
“它不像@p1”,
新的ObjectParameter(“p0”,string.Format(“%{0}%”,NameField)),
新的ObjectParameter(“p1”,string.Format(“%{0}%”,Value)).ToList();
}
错误:

查询语法无效。短期'@p0',第6行,第7列


字段必须是静态的。不能在字段名中使用通配符。此扩展只在内部构建实体SQL查询。实体SQL遵循与普通SQL相同的规则

编辑:

正确的代码是:

public List<tblBook> GetBook(string NameField, string Value)
{
    return this.Entities.Book.Where(
               String.Format("it.{0} NOT LIKE @p0", NameField),
               new ObjectParameter("p0", string.Format("%{0}%", Value))).ToList();
    }
}
公共列表GetBook(字符串名称字段,字符串值)
{
返回此.Entities.Book.Where(
Format(“它{0}不像@p0”,NameField),
新的ObjectParameter(“p0”,string.Format(“%{0}%”,Value)).ToList();
}
}

您必须传递整个字段的名称,并且必须验证它-实体SQL注入也存在。

否您必须明确定义要比较的字段,如果要比较多个字段,则必须在这些条件中使用正确的逻辑运算符。您当前的方法根本不是有效的查询。