这就是我这样做的方式:
public IQueryable<ClientEntity> GetClients(Expression<Func<ClientModel, bool>> criteria)
{
return (
from model in Context.Client.AsExpandable()
where criteria.Invoke(model)
select new Ibfx.AppServer.Imsdb.Entities.Client.ClientEntity()
{
Id = model.Id,
ClientNumber = model.ClientNumber,
NameFirst = model.NameFirst,
//more propertie here
}
);
}
您传入的Expression参数将是您将使用不同的 WHERE 子句、JOINS 等构建的动态查询。此表达式将在运行时被调用并为您提供所需的内容。
这是如何调用它的示例:
public IQueryable<ClientEntity> GetClientsWithWebAccountId(int webAccountId)
{
var criteria = PredicateBuilder.True<ClientModel>();
criteria = criteria.And(c => c.ClientWebAccount.WebAccountId.Equals(webAccountId));
return GetClients(criteria);
}