Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/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# 否定表达式_C#_Linq_Expression - Fatal编程技术网

C# 否定表达式

C# 否定表达式,c#,linq,expression,C#,Linq,Expression,我有一个返回表达式的函数,我传入一个字段和一个值。我有返回StartsWith的函数,但我想返回notstartswith private Expression<Func<T, bool>> GenerateFieldDoesNotStartsWithExpression<T>(string fieldName, string value) { var parameter = Expression.Parameter(typeof(T), "i");

我有一个返回表达式的函数,我传入一个字段和一个值。我有返回StartsWith的函数,但我想返回notstartswith

private Expression<Func<T, bool>> GenerateFieldDoesNotStartsWithExpression<T>(string fieldName, string value) {
    var parameter = Expression.Parameter(typeof(T), "i");
    var fieldAccess = Expression.PropertyOrField(parameter, fieldName);
    MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
    var passedValue = Expression.Constant(value, typeof(string));
    var body = Expression.Call(fieldAccess, method, passedValue);
    var expr = Expression.Lambda<Func<T, bool>>(body, parameter);
    return expr;
}
private Expression generateField不启动xpression(字符串字段名、字符串值){
var参数=表达式参数(类型为(T),“i”);
var fieldAccess=Expression.PropertyOrField(参数,字段名);
MethodInfo method=typeof(string).GetMethod(“StartsWith”,new[]{typeof(string)});
var passedValue=表达式.常量(值,类型(字符串));
var body=Expression.Call(fieldAccess、method、passedValue);
var expr=Expression.Lambda(主体,参数);
返回表达式;
}
返回
i.[fieldName].StartsWith(value)
但我正在查找
!i、 [fieldName].StartsWith(值)

我试过一些方法,比如设置
parameter=!i
和第二个参数为
i
,但随后我得到“参数”!i“未绑定”


我试着在表达式上乱搞。没有,而且似乎无法让它工作。

包围你
表达式。用
表达式调用
。没有

var body = Expression.Not(Expression.Call(fieldAccess, method, passedValue));
您的代码:

    private Expression<Func<T, bool>> GenerateFieldDoesNotStartsWithExpression<T>(string fieldName, string value)
    {
        var parameter = Expression.Parameter(typeof(T), "i");
        var fieldAccess = Expression.PropertyOrField(parameter, fieldName);
        MethodInfo method = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
        var passedValue = Expression.Constant(value, typeof(string));
        // Wrapped Expression.Call with Expression.Not
        var body = Expression.Not(Expression.Call(fieldAccess, method, passedValue));
        var expr = Expression.Lambda<Func<T, bool>>(body, parameter);
        return expr;
    }
private Expression generateField不启动xpression(字符串字段名、字符串值)
{
var参数=表达式参数(类型为(T),“i”);
var fieldAccess=Expression.PropertyOrField(参数,字段名);
MethodInfo method=typeof(string).GetMethod(“StartsWith”,new[]{typeof(string)});
var passedValue=表达式.常量(值,类型(字符串));
//包装表达式。使用表达式调用。不是
var body=Expression.Not(Expression.Call(fieldAccess,method,passedValue));
var expr=Expression.Lambda(主体,参数);
返回表达式;
}
测试代码:

[TestFixture]
public class ExpressNotTests
{
    [Test]
    public void GenerateFieldDoesNotStartsWithExpression_DoesNotStartWith_True()
    {
        var a = new TestClass() {TestString = "Not"};

        var exp = GenerateFieldDoesNotStartsWithExpression<TestClass>("TestString", "Test");

        var res = exp.Compile()(a);

        res.Should().BeTrue();
    }

    [Test]
    public void GenerateFieldDoesNotStartsWithExpression_DoesStartsWith_False()
    {
        var a = new TestClass() {TestString = "TestString"};

        var exp = GenerateFieldDoesNotStartsWithExpression<TestClass>("TestString", "Test");

        var res = exp.Compile()(a);

        res.Should().BeFalse();
    }


    private class TestClass
    {
        public string TestString { get; set; }
    }
}
[TestFixture]
公共类测试
{
[测试]
public void generateFieldDoesNotStartSwitchExpression_DoesNotStartWith_True()
{
var a=newtestclass(){TestString=“Not”};
var exp=generateField不启动表达式(“测试字符串”、“测试”);
var res=exp.Compile()(a);
res.Should().BeTrue();
}
[测试]
public void generatefield未开始表达式\u doestartswith\u False()
{
var a=newtestclass(){TestString=“TestString”};
var exp=generateField不启动表达式(“测试字符串”、“测试”);
var res=exp.Compile()(a);
res.Should().BeFalse();
}
私有类TestClass
{
公共字符串TestString{get;set;}
}
}

向我们展示您在
表达式中使用的代码。Not
,并特别解释哪些代码不起作用。我真的不确定传递给表达式的是什么。Not,也不知道在哪里传递。尝试传递参数(无法转换)parameter=Expression.Not(参数);尝试传递结果表达式(无法转换)var expr=expression.Lambda(主体,参数);expr=Expression.Not(expr)文档将告诉您应该传递给它的内容,并且还提供了一些示例。您可以在任何表达式上调用它,以获得表示的逻辑逆的表达式。似乎我要将其应用于passedValue var passedValue=expression.Not(expression.Constant(value,typeof(string));未为System.Strings定义I get Not。如果正确,则不能不使用字符串。您只能选择一个布尔值。您使用的哪些表达式表示生成布尔值的表达式?