我已经在 asp.net 8 core 中为 jquery 数据表后端创建了通用方法,这是有效的,但我想将其转换为 EF quirybuilder 样式而不是自定义 sql 查询
我尝试了很多,但面临着在where条件下获取动态字段名称的问题
当前工作代码是
public async Task<IActionResult>getdata()
{
var queryString = Request.QueryString.ToString();
IDictionary<string, StringValues> qs = QueryHelpers.ParseQuery(queryString);
var entityType = _context.Model.FindEntityType(typeof(Gate));
//var entityType = typeof(DTGate);
var cols = entityType.GetProperties().Select(p => p.Name).ToArray();
string qry = "select * from " + entityType.GetTableName() + " where 1=1 ";
string mainConds = " and created_by="+User.Identity.UserId();
//ordering
string orderBy = "";
if (qs.ContainsKey("iSortingCols") && !string.IsNullOrEmpty(qs["iSortingCols"]))
{
var numOfSortCols = Convert.ToInt32(qs["iSortingCols"]);
List<string> dtOrders = new List<string>();
for (int i = 0; i < numOfSortCols; i++)
{
dtOrders.Add(qs["mDataProp_" + Convert.ToInt32(qs["iSortCol_" + i])] + " " + qs["sSortDir_" + i]);
}
if (dtOrders.Count > 0)
{
orderBy = " ORDER BY " + string.Join(",", dtOrders);
}
}
//main filter
List<string> filterCondsList = new List<string>();
if (qs.ContainsKey("sSearch") && !string.IsNullOrEmpty(qs["sSearch"]))
{
var searchTerm = qs["sSearch"];
foreach (var col in cols)
{
string thisCond = " " + col + " LIKE '%" + searchTerm + "%'";
filterCondsList.Add(thisCond);
}
}
string filterConds = "";
if (filterCondsList.Count > 0)
{
filterConds = "(" + string.Join(" OR ", filterCondsList) + ")";
}
//column level filtering
string colConds = "";
int qsColumns = Convert.ToInt32(qs["iColumns"]);
for (int i = 0; i < qsColumns; i++)
{
var searchableKey = "bSearchable_" + i;
var cSearchTermIndx = "sSearch_" + i;
if (qs.ContainsKey(searchableKey) && qs[searchableKey] == "true" && qs.ContainsKey(cSearchTermIndx) && !string.IsNullOrEmpty(qs[cSearchTermIndx]))
{
colConds += " AND " + qs["mDataProp_" + i] + " LIKE '%" + qs[cSearchTermIndx] + "%'";
}
}
if (colConds != "")
{
colConds = colConds.Substring(4);
}
//prepare execute sql query
string sqlQry = qry + mainConds;
//int totalRecords = await getTototalRecordsAsync(sqlQry);
int totalRecords = await _recordCountService.GetTotalRecordsAsync(sqlQry);
if (!string.IsNullOrEmpty(filterConds))
{
sqlQry += " AND " + filterConds;
}
if (!string.IsNullOrEmpty(colConds))
{
sqlQry += " AND " + colConds;
}
int recordsFiltered = await _recordCountService.GetTotalRecordsAsync(sqlQry);
if (!string.IsNullOrEmpty(orderBy))
{
sqlQry += orderBy;
}
int displaySkip = Convert.ToInt32(qs["iDisplayStart"]);
int displayLength = Convert.ToInt32(qs["iDisplayLength"]);
sqlQry += " OFFSET " + displaySkip + " ROWS FETCH NEXT " + displayLength + " ROWS ONLY";
var gates = _context.Database.SqlQueryRaw<DTGate>(sqlQry).ToList();
DataTableResponse<DTGate> response = new DataTableResponse<DTGate>();
response.draw = qs["sEcho"];
response.recordsTotal = totalRecords;
response.recordsFiltered = recordsFiltered;
response.data = gates;
var json = JsonConvert.SerializeObject(response);
return Content(json, "application/json");
}
在上面的代码中没有硬编码输入字段名称,所有字段条件都是动态生成的。
你有两个选择。
只需调用 EF core ExecuteSqlRaw 并将其序列化为您认为可以的任何内容。
创建一个包含 800 个属性或您认为可能发生的其他属性的类。你必须添加所有内容。然后你就可以以正确的方式使用 EF-core,只是永远不要从模型创建数据库,或者使用它来获取对象而不指定你想要的列。