在C#中使用Linq删除sql行

Ush*_*her 2 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

D S*_*ley 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);
Run Code Online (Sandbox Code Playgroud)

或明确提取一条记录:

//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
Run Code Online (Sandbox Code Playgroud)