如何使用条件删除Sql中的所有记录 [英] How Do I Delete All Records In Sql Using Condition

查看:133
本文介绍了如何使用条件删除Sql中的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网页表单。我有按钮删除所有记录对一个日期。表格中有两个字段,一个下拉列表中给出了所有扇区,并且是一个日期。当用户选择一个扇区时在字段中输入日期,然后单击删除按钮,然后将删除所有记录。#code如下,我想要linq语句,其中我可以使用where子句。

i have web form .in which i have button to delete all records against one date.there are two fields in the form,one drop down list where all sector are given,and is a date.when user select a sector and put a date in the field,and click on delete button then all record will be deleted.c# code is as follow,i want linq statement in which i can use where clause.

protected void Clear_Roaster(object sender, EventArgs e)
      {
          int sectorId = SafeConvert.ToInt32(ddlsectorRoaster.SelectedValue);
          DateTime dt = SafeConvert.ToDateTime(txtDate.Text);

         int deleteRecord = _service.GetAllBeatStatus().Where(x => x.Date == dt).Count();
         if (deleteRecord != null)
         {
             _service.DeleteBeatStatus(deleteRecord);
         }
         else
         {
             MsgLitteral.ShowNotificationInline("No Record Found!.", true);

         }



只是基于日期假部门下拉


just base on date leave sector drop down

推荐答案

var service = from a in _service
               where a.SectorID == sectorId 
               select a;
_service.RemoveAll(service);
_service.SubmitChanges();


如果你需要在where where条件中包含Date,那么发送日期作为参数DeleteEmployeeStatus方法并更新方法签名

if you need to include Date in your where condition then send date as parameter DeleteEmployeeStatus method and update the method signature
_service.DeleteEmployeeStatus(sectorId, dt);



那么你需要像<$ c $这样的条件c>其中a.SectorID == sectorId和a.ToDate = dt 但当您使用数据库 DateTime 值时> Date / DateTime 在实体框架中,您需要小心代码。请阅读以下文章,了解您可以尝试的几种方法

LINQ查询仅比较DateTime的日期部分 [ ^ ]


then you need both conditions like where a.SectorID == sectorId and a.ToDate = dt but when you are working with DateTime value with Database Date/DateTime in entity framework you need to be careful with the code. Read below article for few ways you can try
LINQ query to compare only date part of DateTime[^]


这篇关于如何使用条件删除Sql中的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆