2

在我的 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)我如何才能获得帖子,例如,只有特定日期(日期时间)或 ID(int)?
  • 2)有没有办法从数据库中删除帖子?
4

2 回答 2

1

1)您应该在您的 LINQ 语句中添加“where”。像这样:

from emp in context.Tasks select emp where emp.Date == new DateTime(2011, 11, 11)

2)要从数据库中删除帖子,您应该执行三个简单的步骤:

  1. 获取要从数据库中删除的帖子

    tasks = from emp in context.Tasks select emp where emp.Date > new DateTime(2011, 11, 11)

  2. 使用我们的删除任务调用 DataContext 对象的 DeleteAllOnSubmit 方法

    dbContext.DeleteAllOnSubmit(tasks);

  3. 调用 DataContext 对象的 SubmitChahges 方法。

    dbContext.SubmitChanges();

于 2012-02-19T07:51:31.340 回答
1

尝试这个:

tasks = from emp in context.Tasks 
        where emp.ID == yourId
        select emp;

要从 te 数据库中删除帖子,请使用DeleteOnSubmit(entity)以下方法:

context.Tasks.Attach(entityToDelete);
context.Tasks.DeleteOnSubmit(entityToDelete);
context.SubmitChanges();
于 2012-02-19T07:39:43.437 回答