我正在开发一个使用 Dapper、使用通用模型的 DapperExtensions 的项目,我想知道如何使用 DapperExtension.GetAll 方法填充模型?
下面是返回我尝试使用 DapperExtensions 过滤的记录的 sql 代码。
select f.*
from Item f
where f.CurrentStatus = 'Open'
AND f.ItemID not in (SELECT ItemID FROM ItemLog l WHERE f.ItemID = l.ItemID
AND l.Status != 'Escalated'
AND DateLogged <= DATEADD(mi, 25, GetDate())) //<- this value would be replaced with a variable
我做了一些研究,发现您可以使用 Split.on,但不确定这是否适合这种情况
GetAll 方法看起来像这样,所以我们确实有能力过滤记录
public virtual IEnumerable<TModel> GetAll(IList<DbFilter<TModel>> filters = null)
{
filters = filters ?? new List<DbFilter<TModel>>();
using (var db = Context)
{
var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
foreach (var filter in filters)
{
pg.Predicates.Add(Predicates.Field(filter.FilterExpression, filter.FilterOperator, filter.FilterItem));
}
return db.GetList<TModel>(pg).ToList();
}
}
任何帮助将不胜感激。我已经接受了创建一个 SPROC 来填充模型的想法。只是试图确定最有效的路线。
好吧,我设法使用以下方法填充我的模型,仍然希望听到反馈或可能的建议。
public async Task<IEnumerable<FormsFnol>> GetLateItems(DateTime responseTime)
{
IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var items = await db.QueryAsync<FormsFnol>(@"
SELECT f.*
FROM Item f
WHERE f.CurrentStatus = 'Open'
AND f.ItemID not in (SELECT ItemID FROM ItemLog l WHERE f.ItemID = l.ItemID
AND l.Status != 'Escalated'
AND DateLogged <= @dateTime
", new { @dateTime = responseTime});
return items;
}