在C#中使用Linq删除sql行

时间:2014-02-25 22:59:13

标签: c# sql linq

我试图从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中的记录的正确方法

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

我刚刚在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);
                        }