从数据库中选择性获取和删除帖子
在我的 Windows Phone 7 应用程序中,我有由 sqlmetal 生成的数据库类。 此外,我还有有助于使用该数据库的课程。
public static IList<Task> GetTasks()
{
IList<Task> tasks = new List<Task>();
using (var context = new MyDBContext(ConnectionString))
{
tasks = (from emp in context.Tasks select emp).ToList();
}
return tasks;
}
此代码返回数据库中的所有帖子。
我的问题:
- 1) 我如何获取帖子,例如仅包含特定日期 (datetime) 或 ID(int) 的帖子?
- 2) 有没有办法从数据库中删除帖子?
In my windows phone 7 app i have database-class generated by sqlmetal.
in addition, i have class that helps to work with this database.
public static IList<Task> GetTasks()
{
IList<Task> tasks = new List<Task>();
using (var context = new MyDBContext(ConnectionString))
{
tasks = (from emp in context.Tasks select emp).ToList();
}
return tasks;
}
this code return all posts from the database.
My questions:
- 1) How I can get posts, for example, only with a specific date (datetime) or ID(int)?
- 2) Is there any way to delete posts from the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
要从数据库中删除帖子,请使用
DeleteOnSubmit(entity)
方法如下:
Try this:
To delete posts from te databse use
DeleteOnSubmit(entity)
method like:1) 您应该将“where”添加到 LINQ 语句中。像这样:
2)要从数据库中删除帖子,您应该执行三个简单的步骤:
获取要从数据库中删除的帖子
tasks = from emp in context.Tasks select emp where emp.Date > > new DateTime(2011, 11, 11)
调用 DataContext 对象的 DeleteAllOnSubmit 方法来删除我们的任务
dbContext.DeleteAllOnSubmit(tasks);
调用 DataContext 对象的 SubmitChahges 方法。
dbContext.SubmitChanges();
1) You should add 'where' to your LINQ statement. Like this:
2) To delete posts from database you should do three simple steps:
Get posts you want to delete from db
tasks = from emp in context.Tasks select emp where emp.Date > new DateTime(2011, 11, 11)
Call DeleteAllOnSubmit method of your DataContext object with our tasks to delete
dbContext.DeleteAllOnSubmit(tasks);
Call SubmitChahges method of your DataContext object.
dbContext.SubmitChanges();