C# Expression to use Guid bookmark

前端 未结 1 743
北荒
北荒 2021-01-22 04:26

I have a requirement to work through several tables that need to be synchronized/backed up. All of these tables\' classes implement ITrackModifiedDate:



        
相关标签:
1条回答
  • 2021-01-22 05:03

    The general approach is to replace the unsupported Guid operators with Guid.CompareTo(Guid) calls, e.g. instead of

    guidA > guidB
    

    use

    guidA.CompareTo(guidB) > 0
    

    In your case, replace

     var greaterThanMethod = Expression.GreaterThan(propertyExp, target);
    

    with

    var compareTo = Expression.Call(propertyExp, "CompareTo", Type.EmptyTypes, target);
    var greaterThanMethod = Expression.GreaterThan(compareTo, Expression.Constant(0));
    

    This works for most of the query providers (LINQ to Objects, LINQ To Entities (EF6)). Unfortunately doesn't work with EF Core 2.x which requires a different approach. If that's the case, register the custom GuidFunctions class as explained in the linked answer, and use this instead:

    var greaterThanMethod = Expression.Call(
        typeof(GuidFunctions), "IsGreaterThan", Type.EmptyTypes,
        propertyExp, target);
    
    0 讨论(0)
提交回复
热议问题