gpt4 book ai didi

c# - 在 C# 中使用 Linq 删除 sql 行

转载 作者:行者123 更新时间:2023-11-30 19:40:41 24 4
gpt4 key购买 nike

我正在尝试通过 Linq 从 C# 中删除一个 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

关于c# - 在 C# 中使用 Linq 删除 sql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22028078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com