C# 在C中使用Linq删除sql行
我正在尝试从C到Linq删除sql表记录,但由于某些原因,无法重新组织DeleteonSubmit,我不确定我缺少了什么,请指导我正确的方法 这是我的密码C# 在C中使用Linq删除sql行,c#,sql,linq,C#,Sql,Linq,我正在尝试从C到Linq删除sql表记录,但由于某些原因,无法重新组织DeleteonSubmit,我不确定我缺少了什么,请指导我正确的方法 这是我的密码 proxy = new ServiceClient("basicHttp_IService"); //GatewayService.ServiceClient proxy = new ServiceClient("basicHttp_IService"); SearchCriteria c
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.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);
}
如果在分配storytodelete后停止,你看到记录了吗?我没有看到记录,我可以看到在storytodelete列表lstStoriestoDelete=来自storytodelete中的story后添加此代码的记录在story.ProgramId等于program.Id的程序中加入story.ProfileId上的程序中加入程序等于profile.Id选择new Story ProgramStory,program,profile.ToList;我的问题是,它在DeleteonSubmit或DeleteAllonSubmit系统上引发异常。数组不包含DeleteAllonSubmit的定义,并且没有扩展方法DeleteAllonSubmit接受System类型的第一个参数。数组是否缺少using指令或程序集引用请显示正在定义故事的位置,这不应该是一个错误array@aw04,更新了我的op。我打电话给我的wcf服务获取故事谢谢你的评论,现在我的问题是,它在DeleteonSubmit或DeleteAllonSubmit系统上引发异常。数组不包含DeleteAllonSubmit的定义,并且没有扩展方法DeleteAllonSubmit接受System.ArrayEvent类型的第一个参数。我使用System.Linq添加了命名空间;使用System.Data.Sql;C区分大小写。检查您的大小写。intellisense不显示类似于在故事之后删除的选项。好了,现在您添加了更多上下文,我可以看到故事是从代理返回的某种集合。你的代理上有某种类型的Delete方法吗?是的,它肯定返回数组。谢谢你的建议,你建议将集合类型更改为Generic.list吗?不,没有那么简单。您需要与数据库表相关的实际类型。
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);
}