public async Task CreateDep(string wxid) { if (await _dbConn_Biz.Queryable<BmAfferentEntity>().Where(x => x.f_lx == 0 && x.f_id == wxid).AnyAsync()) return; BmAfferentEntity _entity = new() { f_lx = 0, f_id = wxid, f_jdrq = _dbConn_Biz.GetDate().ToString("yyyy-MM-dd HH:mm:ss") }; await _dbConn_Biz.Insertable(_entity).ExecuteCommandAsync(); }
时间: 2024-04-02 16:36:20 浏览: 178
这是一段 C# 代码,是创建一个异步方法 CreateDep,用来向数据库中的 BmAfferentEntity 表中插入一条数据。具体来说,代码中的意思是,首先判断 BmAfferentEntity 表中是否已存在 f_lx 等于 0 且 f_id 等于 wxid 的数据,如果存在则直接返回,不执行插入操作;如果不存在,则创建一个 BmAfferentEntity 对象 _entity,并给其属性赋值,然后使用 Insertable 方法将 _entity 对象插入到数据库中,最后使用 ExecuteCommandAsync 方法执行插入操作。该方法返回一个 Task 对象,表示异步操作的结果。
相关问题
public async Task<List<Entity>> QueryWhereAsync(Expression<Func<Entity, bool>> where) { return await db.Queryable<Entity>().Where(where); }注入使用SQL sugar报错,说where不能为null
根据你提供的代码,我发现在使用 SQL Sugar 进行查询时,当 `where` 表达式为 `null` 时会抛出异常。因此,你需要在调用 `QueryWhereAsync` 方法前,确保 `where` 参数不为 `null`。
你可以采用以下方式之一:
1. 在调用 `QueryWhereAsync` 方法前,先进行 `null` 判断,如果为 `null`,则给 `where` 表达式一个默认值。
```
public async Task<List<Entity>> QueryWhereAsync(Expression<Func<Entity, bool>> where)
{
if (where == null)
{
where = x => true; // 给 where 表达式一个默认值
}
return await db.Queryable<Entity>().Where(where).ToListAsync();
}
```
2. 在调用 `QueryWhereAsync` 方法时,将 `where` 参数设置为一个默认值。
```
var list = await QueryWhereAsync(null);
```
建议采用第一种方式,这样可以保证代码的健壮性。
public async Task<pms_plan> UpdateOrder(string plaSsn, string direction) { if (string.IsNullOrEmpty(plaSsn) || string.IsNullOrEmpty(direction)) { return null!; } var plsn = await _sqlSugarClient.Queryable<pms_plan>().FirstAsync(s => s.sn == plaSsn); if (plsn == null) { return null!; } var PendingList = await _sqlSugarClient.Queryable<pms_plan>() .OrderBy(it => new { it.plan_date, it.list_order }) .ToListAsync(); int inden = PendingList.FindIndex(i => i.sn == plsn.sn); if (direction == "up") { var plan1 = PendingList[inden - 1]; int i = 0; i = (int)plan1.list_order!; plan1.list_order = plsn.list_order; plsn.list_order = i; _sqlSugarClient.Updateable(plan1).WhereColumns(it => new { it.id }).ExecuteCommand();//更新单 条根据ID } if (direction == "down") { var plan1 = PendingList[inden + 1]; int i = 0; i = (int)plan1.list_order!; plan1.list_order = plsn.list_order; plsn.list_order = i; _sqlSugarClient.Updateable(plan1).WhereColumns(it => new { it.id }).ExecuteCommand();//更新单 条根据ID } _sqlSugarClient.Updateable(plsn).WhereColumns(it => new { it.id }).ExecuteCommand();//更新单 条根据ID return plsn; }优化代码
这是一段 C# 代码,用于更新数据库中的 pms_plan 表中的记录的顺序。如果传入的参数 plaSsn 或 direction 为空,则返回 null。如果找不到与 plaSsn 相匹配的记录,则也返回 null。如果 direction 为 "up",则将该记录与前一条记录的顺序互换;如果 direction 为 "down",则将该记录与后一条记录的顺序互换。最后,更新该记录的顺序,并返回该记录。
对于这段代码,可以进行如下优化:
1. 首先,可以将查询数据库的操作放在一个事务中,以提高效率和数据一致性。
2. 其次,可以使用异步操作来提高代码的性能。
3. 可以使用 LINQ 表达式来简化代码,使其更易读、易懂。
4. 可以使用更加严谨的类型检查和异常处理,以提高代码的健壮性和可维护性。
下面是优化后的代码:
public async Task<pms_plan> UpdateOrder(string plaSsn, string direction)
{
if (string.IsNullOrEmpty(plaSsn) || string.IsNullOrEmpty(direction))
{
return null;
}
using (var tran = await _sqlSugarClient.Ado.BeginTranAsync())
{
try
{
var plsn = await _sqlSugarClient.Queryable<pms_plan>().FirstAsync(s => s.sn == plaSsn);
if (plsn == null)
{
return null;
}
var PendingList = await _sqlSugarClient.Queryable<pms_plan>()
.OrderBy(it => new { it.plan_date, it.list_order })
.ToListAsync();
int inden = PendingList.FindIndex(i => i.sn == plsn.sn);
if (direction == "up")
{
if (inden > )
{
var plan1 = PendingList[inden - 1];
int i = (int)plan1.list_order!;
plan1.list_order = plsn.list_order;
plsn.list_order = i;
await _sqlSugarClient.Updateable(plan1).WhereColumns(it => new { it.id }).ExecuteCommandAsync();
}
}
else if (direction == "down")
{
if (inden < PendingList.Count - 1)
{
var plan1 = PendingList[inden + 1];
int i = (int)plan1.list_order!;
plan1.list_order = plsn.list_order;
plsn.list_order = i;
await _sqlSugarClient.Updateable(plan1).WhereColumns(it => new { it.id }).ExecuteCommandAsync();
}
}
await _sqlSugarClient.Updateable(plsn).WhereColumns(it => new { it.id }).ExecuteCommandAsync();
await tran.CommitAsync();
return plsn;
}
catch (Exception ex)
{
await tran.RollbackAsync();
throw ex;
}
}
}
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)