Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.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# 创建调用类型为nullable的方法的表达式树_C#_Expression Trees - Fatal编程技术网

C# 创建调用类型为nullable的方法的表达式树

C# 创建调用类型为nullable的方法的表达式树,c#,expression-trees,C#,Expression Trees,我希望通过使用表达式树来实现此结果: x.DataMod==null?string.Empty:x.DataMod.Value.ToShortDateString() 我正在尝试使用Expression.Condition,但我不知道如何调用ToSortDateString() 我需要LINQGroupBy查询中的结果 var grouped = context.Receipt.GroupBy(x => (x.DataMod == null ? string.Empty : x.DataM

我希望通过使用表达式树来实现此结果:

x.DataMod==null?string.Empty:x.DataMod.Value.ToShortDateString()

我正在尝试使用
Expression.Condition
,但我不知道如何调用
ToSortDateString()

我需要LINQGroupBy查询中的结果

var grouped = context.Receipt.GroupBy(x => (x.DataMod == null ? string.Empty : x.DataMod.Value.ToShortDateString()) + ";" + x.DataOraDocumento.ToShortDateString() + ";" + x.Cassa.Descrizione + ";" + x.Sezionale.Descrizione).ToList();

谢谢

如评论中所建议的,您应该使用
Expression.Call
ToSortDateString
方法调用创建一个表达式。我已经为原始表达式的稍微简化版本编写了一个示例。请参阅内联注释

x.DataMod.HasValue?x、 DataMod.Value.ToShortDateString():string.Empty

void Main()
{
    var now = DateTime.UtcNow;
    var tomorrow = now.AddDays(1);

    var data = new Test[] {
        new Test { DataMod = null },
        new Test { DataMod = now },
        new Test { DataMod = null },
        new Test { DataMod = tomorrow },
        new Test { DataMod = null },
        new Test { DataMod = now }
    };

    var keySelector = BuildSelector();

    var groups = data
        .GroupBy(keySelector)
        .ToList();

    foreach (var group in groups)
    {
        Console.WriteLine($"Group = \"{group.Key}\"");
        Console.WriteLine($"Items = {group.Count()}");
        Console.WriteLine();
    }
}

class Test
{
    public DateTime? DataMod { get; set; }
}

Func<Test, string> BuildSelector()
{
    // Builds delegate for the Expression: 
    // x => x.DataMod.HasValue ? x.DataMod.Value.ToShortDateString() : string.Empty

    // Expression for: x
    var x = Expression.Parameter(typeof(Test), "x");

    // Expression for: x.DataMod.HasValue
    var testExpr = Expression.Property(
        Expression.Property(
            x, 
            nameof(Test.DataMod)
        ),
        nameof(Nullable<DateTime>.HasValue)
    );

    // Expression for: x.DataMod.Value.ToShortDateString()
    var ifTrueExpr = Expression.Call(

        // Expression for: x.DataMod.Value
        Expression.Property(
            Expression.Property(
                x,
                nameof(Test.DataMod)
            ),
            nameof(Nullable<DateTime>.Value)
        ),

        // Expression for: ToShortDateString
        typeof(DateTime).GetMethod(
            nameof(DateTime.ToShortDateString)
        )

        /* args if any */
    );

    // Expression for: string.Empty
    var ifFalseExpr = Expression.Field(
        null,
        typeof(string).GetField(nameof(string.Empty))
    );

    // Expression for: x => x.DataMod.HasValue ? x.DataMod.Value.ToShortDateString() : string.Empty
    var conditionExpr = Expression.Condition(
        testExpr, 
        ifTrueExpr, 
        ifFalseExpr);

    // Compile to a delegate
    var lambda = Expression.Lambda<Func<Test, string>>(conditionExpr, x);
    return lambda.Compile();
}
void Main()
{
var now=DateTime.UtcNow;
var明天=现在.AddDays(1);
var数据=新测试[]{
新测试{DataMod=null},
新测试{DataMod=now},
新测试{DataMod=null},
新测试{DataMod=tomory},
新测试{DataMod=null},
新测试{DataMod=now}
};
var keySelector=BuildSelector();
变量组=数据
.GroupBy(键选择器)
.ToList();
foreach(组中的var组)
{
Console.WriteLine($“Group=\”{Group.Key}\”);
WriteLine($“Items={group.Count()}”);
Console.WriteLine();
}
}
课堂测试
{
公共日期时间?DataMod{get;set;}
}
Func BuildSelector()
{
//为表达式生成委托:
//x=>x.DataMod.HasValue?x.DataMod.Value.ToSortDateString():string.Empty
//表达式:x
var x=表达式参数(类型(测试),“x”);
//表达式:x.DataMod.HasValue
var testExpr=Expression.Property(
表达式.属性(
x,,
名称(Test.DataMod)
),
nameof(Nullable.HasValue)
);
//表达式:x.DataMod.Value.ToShortDateString()
var-iftruexpr=Expression.Call(
//表达式:x.DataMod.Value
表达式.属性(
表达式.属性(
x,,
名称(Test.DataMod)
),
nameof(可为null.Value)
),
//表达式:ToSortDateString
typeof(DateTime).GetMethod(
名称(DateTime.ToSortDateString)
)
/*如果有的话*/
);
//表达式:string.Empty
var ifFalseExpr=Expression.Field(
无效的
typeof(string.GetField)(nameof(string.Empty))
);
//表达式:x=>x.DataMod.HasValue?x.DataMod.Value.ToShortDateString():string.Empty
var conditionExpr=Expression.Condition(
testExpr,
如果真是这样,
ifFalseExpr);
//编译为委托
var lambda=Expression.lambda(conditionExpr,x);
返回lambda.Compile();
}

它只是一个与其他方法一样的方法调用:)使用
Value
属性(
Expression.property(nullable\u expr,“Value”)
而不是
nullable\u expr