简体   繁体   English

如何从表格中删除元素?

[英]How do I remove elements from a table?

Allow me to explain my situation. 请允许我解释一下我的情况。

I have a table in my database represented in my model by 我的模型中的数据库中有一个表,由

   public Table<LinkDate> dates;

   [Table(Name = "dates")]
   public class LinkDate
   {
       public LinkDate()
       {

       }

       [Column(IsPrimaryKey = true)]
       public string linkguid { get; set; }
       [Column]
       public DateTime dtime { get; set; }

   }

which records the day/time that certain IDs were generated. 记录生成特定ID的日期/时间。 These IDs are associated with one or more files in a table represented by 这些ID与由表示的表中的一个或多个文件相关联

[Table( Name = "links")]
public class AssetLink
{
    public AssetLink()
    {

    }

    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int linkid { get; set; }
    [Column]
    public string linkguid { get; set; }
    [Column]
    public int fileid { get; set; }

}

I'm adding a functionality in my web application where a user can delete all links that are over a certain number of days old. 我在Web应用程序中添加了一项功能,用户可以在其中删除超过一定天数的所有链接。 Which means that if any such links exist, I will be deleting rows from both links and dates . 这意味着,如果存在任何此类链接,我将从linksdates删除行。

The action I've started to create is 我开始创建的动作是

    [HttpPost]
    public ActionResult FlushLinks (string numDaysOld)
    {
        // Deletes all database references to links were submitted over numDaysOld days ago

        DateTime currentDateTime = DateTime.Now;
        foreach (LinkDate thisLinkDate in PD.dates)
        {
            TimeSpan thisTimeSpan = new TimeSpan(Convert.ToInt16(numDaysOld), 0, 0, 0);
            if ((currentDateTime - thisLinkDate.dtime) > thisTimeSpan)
            {
                 // ...
            }

        }

        try 
        {
            PD.SubmitChanges();
        }
        catch (Exception ex)
        {
            return Content(ex.Message, "text/html");
        }

        // if here, everything went well
        return Content("Successfully removed all old links!", "text/html");
    }

and what I need help filling out is the // ... part because I don't know how to, and can't see any documentation about a way to , delete rows. 我需要帮助的地方是// ...部分,因为我不知道如何, 也看不到有关删除行的方法的任何文档 What I'm trying to do is the equivalent of 我想做的就是

 PD.links.Remove(x => x.linkguid == thisLinkDate.linkguid); // need something like this if possible
 thisLinkDate.Remove() // and need something like this if possible

What do I need to do? 我需要做什么?

Also, please let me know about any bugs you see in my procedure. 另外,请让我知道您在程序中看到的任何错误。

EDIT: I think I figured it out by myself: 编辑:我想我自己想通了:

foreach (AssetLink thislink in PD.links) if (thislink.linkguid == thisLinkDate.linkguid) PD.links.DeleteOnSubmit(thislink);
PD.dates.DeleteOnSubmit(thisLinkDate); 

If that's wrong, let me know 如果那是错的,让我知道

Try replacing your foreach loop with this. 尝试以此替换您的foreach循环。

DateTime currentDateTime = DateTime.Now;
TimeSpan thisTimeSpan = new TimeSpan(Convert.ToInt16(numDaysOld), 0, 0, 0);
var delete_list = PD.dates.Where(x => (currentDateTime - x.dtime) > thisTimeSpan)
PD.dates.RemoveRange(delete_list);

请注意,您只需要从日期表中删除,这将导致链接表中引用该日期的所有行的级联删除,前提是已为这些表正确配置了数据库的参照完整性检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM