我有一个类似于下面的 API 方法。
是否可以附加 IQueryable 以便最终查询具有执行 where 语句的项目列表,或者我应该使用 SqlQuery 属性并构建原始 SQL 字符串。
public IEnumerable<Message> GetMessagesFromApi(DateTime? dateFrom = null, DateTime? dateTo = null, int flag = -1, int messageType = 1, bool media =false)
{
if (dateFrom == null)
dateFrom = DateTime.MinValue;
if (dateTo == null)
dateTo = DateTime.MaxValue;
IQueryable<Message> query = null;
query = db.Messages.Where(x => x.Created >= dateFrom && x.Created <= dateTo);
if (flag >= 0)
{
//Append the where statement on dates to now include Flag
query = query.Where(x => x.Flag == flag);
}
//Here it should execute against Where date >= and date <= and flag = 1
}