繁体   English   中英

在C#中使用Linq删除sql行

[英]Delete a sql row using Linq in C#

我试图从C#通过Linq删除一个sql表记录但是有些原因DeleteonSubmit没有被重新认识,我不确定我在这里缺少什么,请指导我正确的方法

这是我的代码

          proxy = new ServiceClient("basicHttp_IService");
        //GatewayService.ServiceClient proxy = new ServiceClient("basicHttp_IService");

        SearchCriteria criteria = new SearchCriteria();
        criteria.UserRoles = new string[] { "*" };

        var stories = proxy.GetStoryItemsByCriteria(criteria);
        var programs = proxy.GetPrograms();
        var Profiles = proxy.GetProfiles();
foreach(StoryProgram sp in lstStoriestoClose)
{
    try
    {
        DateTime LastUpdateTimestamp;
        DateTime CurrentTime = DateTime.Now;
        LastUpdateTimestamp = sp.Story.LastUpdatedOn;

        if ((CurrentTime - LastUpdateTimestamp).TotalHours > 24)
        {
            //Delete the story from database
            var storytoDelete = from story in stories
                                where story.Id == sp.Story.Id
                                select story;

            //I am trying to delete the record like below
            stories.DeleteonSubmit(storytoDelete);
            stories.SubmitChanges();
            //Intellisense doesn't show the option DeleteonSubmit and SubmitChanges
        }
    }
}

请指导我通过Linq删除SQL中的记录的正确方法

DeleteOnSubmit适用于单个实体。 您的查询返回一实体(授权可能只有一个条目,但它仍然是一个集合)。 您可以使用DeleteAllOnSubmit

//Delete the story from database
var storytoDelete = from story in stories
                    where story.Id == sp.Story.Id
                    select story;

//I am trying to delete the record like below
stories.DeleteAllOnSubmit(storytoDelete);

或明确提取一条记录:

//Delete the story from database
var storytoDelete = from story in stories
                    where story.Id == sp.Story.Id
                    select story;

//I am trying to delete the record like below
stories.DeleteOnSubmit(storytoDelete.Single());  // or First, depending on whether you expect more than one match

看起来您的服务正在返回一个数组,而不是一个有效的linq数据库对象。 这就是为什么它不能识别您期望看到的方法。 您需要检查类型并从那里开始。

您始终可以右键单击服务引用并配置为检查/设置返回类型。

我刚刚在WCF服务中添加了删除功能,并传递sql记录详细信息以从SQL中删除解决了我的问题的记录。

感谢大家的建议和建议。

 foreach(StoryProgram sp in lstStoriestoClose)
        {
            try
            {
                DateTime LastUpdateTimestamp;
                DateTime CurrentTime = DateTime.Now;
                LastUpdateTimestamp = sp.Story.LastUpdatedOn;
                if ((CurrentTime - LastUpdateTimestamp).TotalHours > 24)
                {
                    //Delete the story from database
                    //Check the gateway to delete the record in the db.
                    var storytoDelete= from story in stories
                                        where story.Id== sp.Story.Id
                                        select story;

                   // stories.DeleteAllonSubmit(storytoDelete);
                    List<StoryProgram> lstStoriestoDelete = (from story in storytoDelete
                                                            join program in programs on story.ProgramId equals program.Id
                                                            join profile in Profiles on story.ProfileId equals profile.Id
                                                            select new StoryProgram(story, program, profile)).ToList();
                    foreach (StoryProgram sps in lstStoriestoDelete)
                    {
                        try
                        {
                            proxy.DeleteStoryItem(sps.Story.Id);
                        }

暂无
暂无

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

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