1

我有一个示例查询类,其中包含查询参数作为其属性。此类用于 WCF 通信,并在服务器端生成适当的过滤器表达式。

示例代码在哪里。

internal interface IExpressionBuilder<T>
{
   Func<T, bool> Build();
}

这是示例实现

[DataContract]
public class PersonQuery : IExpressionBuilder<Person>
{
    [DataMember]
    public string IdCardNumber;
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public DateTime? BirthDate { get; set; }

    #region Implementation of IExpressionBuilder<Data>

    public Func<Person, bool> Build()
    {
        throw new NotImplementedException();
    }

    #endregion
}

用法:

publi List<Person> GetPersonByQuery(PersonQuery query)
{
    (using context = new SampleContext())
    {
        return List<Person> foundPersons = context.people.where(query.build());
    }
}

您可以看到这个想法非常简单,但我的问题是如何在表达式中动态包含 PersonQuery 属性,如果它们已设置,则在调用 build() 方法时,基于

4

1 回答 1

0

Have you looked at Dynamic Linq? It seems ideally suited to your problem.

If you want to use functions and build expression trees, this library provides a nice, fluent syntax for creating expression trees.

于 2012-09-10T15:38:50.233 回答