如何将谓词转换为字符串和字符串谓词

时间:2015-10-21 06:21:26

标签: c# linq c#-4.0 lambda expression-trees

我处理一个特殊的项目,必须将Predicates(或Expressions)转换为字符串并存储在数据库上并检索它并将其转换为Predicates并对其进行评估,因为我想在运行时更改它。请帮我修改ConvertStringToPredicate和ConvertStringToPredicate方法。

public class Program
{
    static void Main(string[] args)
    {
        string predicateStr = GetPredicateFromDb(100);

        Predicate<Account> predicate = ConvertStringToPredicate(predicateStr);

        Account account = new Account();

        var status = account.Evaluate(predicate);

        //...
    }

    public static string GetPredicateFromDb(int id)
    {
        //Get Predicate String From Database
        //...
    }

    public static Predicate<Account> ConvertStringToPredicate(string predicate)
    {
        //???
    }

    public static string ConvertStringToPredicate(Predicate<Account> predicate)
    {
        //???
    }
}

public class Account
{
    public decimal Balance { get; set; }

    public bool Evaluate(Predicate<Account> matchingCriteria)
    {
        //Evaluate Predicate
        //...
    }  
}

1 个答案:

答案 0 :(得分:1)

表达树序列化有几个库:

  1. metalinq
  2. expression tree serializer
  3. serialize linq