前言:临时做了个.net的小项目。增删改成了解一下。
1.增
public bool InsertAllAwardDetaiList(List<AwardDetailsDTO> exo)
{
using (NO1DBContext ntx = new NO1DBContext())
{
//循环体就是list集合(exo.Count)
for (int i = 0; i < exo.Count; i++)
{
//for循环里边的代码就是普通的linq语句的插入方法
string id = Snowflake.GetId().ToString();
//将dto 的值传给实体entity
To_Award_DetailsEntity e = new To_Award_DetailsEntity() {
Id = id, Rule_Id = exo[i].RuleId,
Operator = exo[i].Operator,Organization_Id = exo[i].OrganizationId,
Check_State = 1,
Award_Money=exo[i].AwardMoney,
Award_Rules=exo[i].AwardRules,
Is_Delete = false,
Create_Time = DateTime.Now,
Submit_Time = DateTime.Now,
Update_Time = DateTime.Now,Remark = "待审核"};
ntx.Award_Details.Add(e);
}
ntx.SaveChanges();
}
return true;
}
2.删
public bool DeleteAwardDetailByOrganizationId(string organizationId)
{
using (NO1DBContext ntx = new NO1DBContext())
{
BaseService<To_Award_DetailsEntity> awardDetails = new BaseService<To_Award_DetailsEntity>(ntx);
var deleteAwardDetails = awardDetails.GetAll().Where(delAwardDetails => delAwardDetails.Organization_Id == organizationId).ToList();
for(int i=0;i< deleteAwardDetails.Count;i++)
{
deleteAwardDetails[i].Is_Delete = true;
}
ntx.SaveChanges();
return true;
}
}
3.改(更新)
public bool UpdatePassAwardStateByOrganizationId(string organizationId)
{
using (NO1DBContext cxt = new NO1DBContext())
{
try
{
// 根据期数更新为已通过状态
BaseService<To_Award_DetailsEntity> awardDetails = new BaseService<To_Award_DetailsEntity>(cxt);
var updatePassAwardDetails = awardDetails.GetAll().Where(passAwardDetails => passAwardDetails.Organization_Id ==organizationId).ToList();
for (int i = 0; i < updatePassAwardDetails.Count; i++)
{
updatePassAwardDetails[i].Check_State = 3;
// 如果状态为3则通过审核,没有未通过原因
updatePassAwardDetails[i].Remark = "无";//备注无(审核通过,备注为空)
updatePassAwardDetails[i].Update_Time = DateTime.Now;
updatePassAwardDetails[i].Check_Time = DateTime.Now;// 审核时间
}
cxt.SaveChanges();
return true;
}
catch (Exception ex)
{
// 更新失败,记录日志
log.Error(ex);
return false;
}
}
}
4.查
public List<SearchOrganizationsStausDTO> SelectAllInsertOrganization()
{
using (NO1DBContext ntx = new NO1DBContext())
{
string sql = "SELECT v_organization.organization_id AS OrganizationId,v_organization.organization_name AS OrganizationName,award.check_state AS CheckState FROM v_organization LEFT JOIN(SELECT to_award_details.organization_id, to_award_details.check_state FROM to_award_details WHERE is_delete = 0) AS award ON v_organization.organization_id = award.organization_id WHERE v_organization.organization_id NOT IN(SELECT to_award_details.organization_id FROM to_award_details WHERE is_delete = 0 AND check_state = 1 or is_delete = 0 AND check_state = 3) GROUP BY organization_name ORDER BY LPAD(organization_name,3,0) DESC";
var AllInsertOrganization = ntx.Database.SqlQuery<SearchOrganizationsStausDTO>(sql).ToList();
return AllInsertOrganization;
}
}
总结:代码量需要不断积累,在积累中才能越熟练。