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?
有帮助吗?

解决方案

Try this:

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

To delete posts from te databse use DeleteOnSubmit(entity) method like:

context.Tasks.Attach(entityToDelete);
context.Tasks.DeleteOnSubmit(entityToDelete);
context.SubmitChanges();

其他提示

1) You should add 'where' to your LINQ statement. Like this:

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

2) To delete posts from database you should do three simple steps:

  1. Get posts you want to delete from db

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

  2. Call DeleteAllOnSubmit method of your DataContext object with our tasks to delete

    dbContext.DeleteAllOnSubmit(tasks);

  3. Call SubmitChahges method of your DataContext object.

    dbContext.SubmitChanges();

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top