none
working with a linq query RRS feed

  • Question

  • Hello,
    I have a linq query defined similar to this -

     

    var query = from x in ......

     

    After I run my query, I would like to iterate through and remover certain items based on criteria.  can someone help me with this?

    I thought I could use this -

    foreach (var x in query)  
                {  
                    if (x.sys == "")  
                    {  
                        What goes here to delete??  
                    }  
                } 

     but I dont know how to delete certain rows.

    Thanks for any thoughts.

    Wednesday, March 25, 2009 1:06 PM

Answers

  • If you are selecting a well-known entity from the database, you can use the DataContext.Entities.DeleteOnSubmit method, passing in x as a parameter, where "Entities" is a property on your DataContext that represents the entity you selected.

    If you are a selecting a projection of an entity (e.g. from x in dataContext.Foo select new { x.Bar, x.Baz }), then before deleting the entity you have to ensure that you have all the data that LINQ to SQL needs to resolve the entity in the database. This includes the primary key and either all columns marked as "Time Stamp" in the designer or all columns marked as UpdateCheck.Always (see the "Update Check" property in the property grid when you select a column on the LINQ to SQL designer).

    If you have all that data, in your condition (if x.sys == "") you can create a new entity, set all the properties that fit the description above and then attach it to the data context. Afterwards, you can call DataContext.Entities.DeleteOnSubmit, passing in that entity. Here's an example:

    using (PetShopDataContext db = new PetShopDataContext())
    {
        Product p2 = new Product();
        p2.productId = 1;
        db.Products.Attach(p2);
        db.Products.DeleteOnSubmit(p2);
    
        db.SubmitChanges();
    }
    

    In the example above, the only property I need to set is the productId (the primary key). Note that I had to go to the designer and change all the UpdateCheck.Always properties on Product to be UpdateCheck.Never.
    Sunday, March 29, 2009 3:44 PM
    Answerer

All replies

  • I think you would be better server to return an Entity and not just a var.  A var is great if you are going to bind it to a datasource and you don't care about much else.  If you want to manipulate the data I would use List<T>

    List<T> list = (from s in qs select new T(s)).ToList();


    foreach
    (T s in list)

    {

    //Now do something off of s.

     

    }


    joe alt
    Thursday, March 26, 2009 1:52 PM
  • If you are selecting a well-known entity from the database, you can use the DataContext.Entities.DeleteOnSubmit method, passing in x as a parameter, where "Entities" is a property on your DataContext that represents the entity you selected.

    If you are a selecting a projection of an entity (e.g. from x in dataContext.Foo select new { x.Bar, x.Baz }), then before deleting the entity you have to ensure that you have all the data that LINQ to SQL needs to resolve the entity in the database. This includes the primary key and either all columns marked as "Time Stamp" in the designer or all columns marked as UpdateCheck.Always (see the "Update Check" property in the property grid when you select a column on the LINQ to SQL designer).

    If you have all that data, in your condition (if x.sys == "") you can create a new entity, set all the properties that fit the description above and then attach it to the data context. Afterwards, you can call DataContext.Entities.DeleteOnSubmit, passing in that entity. Here's an example:

    using (PetShopDataContext db = new PetShopDataContext())
    {
        Product p2 = new Product();
        p2.productId = 1;
        db.Products.Attach(p2);
        db.Products.DeleteOnSubmit(p2);
    
        db.SubmitChanges();
    }
    

    In the example above, the only property I need to set is the productId (the primary key). Note that I had to go to the designer and change all the UpdateCheck.Always properties on Product to be UpdateCheck.Never.
    Sunday, March 29, 2009 3:44 PM
    Answerer