Clear All Childs With 1 Query

5 posts, 1 answers
  1. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 23 Sep 2009 Link to this post

    hi
    i have two class ( DCDocumentAdmin and DCGroup ) that i declare a collection field of type DCDocumentAdmiin in DCGroup for make a 1-to-many relation between those , with this , i have a GroupId field in database that is coantain the Related group id for DCDocumentAdmin ,
    now i try to remove the DCDocumentAdmins that have a special GroupId ,
    i do this by removing DCDocumentAdmins one by one in separate queries , ( i write my code for you in below ) :
    (market function as Bold Remove DocumentAdmins One By One with separate queris that this have no good way)

    public DCDocumentAdmin RetrieveDCDocumentAdminById(int DocumentAdminId)
                {
                    try
                    {
                        var query = (from q in scope.Extent<DCDocumentAdmin>()
                                 where q.Id == DocumentAdminId
                                 select q).Single();
                        return query;
                    }
                    catch (Exception ex)
                    {
                       
                        throw;
                    }
                }

    public void RemoveDCDocumentAdminById(int DocumentAdminId)
                {
                    try
                    {
                        DCDocumentAdmin DocumentAdmin = RetrieveDCDocumentAdminById(DocumentAdminId);
                        scope.Transaction.Begin();
                        scope.Remove(DocumentAdmin);
                        scope.Transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                       
                        throw;
                    }
                }

    public void RemoveDCGruopDocumentAdminsByArchiveId(int archiveId)
                {
                    DCGroup archive = RetrieveDCGroupById(archiveId);

                    for (int i = 0; i < archive.DocumentAdmins.Count; i++)
                    {
                        documentAdminManager.RemoveDCDocumentAdminById(archive.DocumentAdmins[i].Id);
                    }
                }


    HOW CAN I REMOVE THE DCDocumentAdmins THAT HAVE A SPECIAL GroupID in one query like below (the way that i do before) :
    ( delete from DCDocumentAdmin where DCDocumentAdmin.GroupId  = @GroupId )

    in the way that i use to make relation between tables in openaccess i have no access to GroupId when i working with DCDocumentAdmin to remove admins in 1 query

    what is my solution
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 24 Sep 2009 Link to this post

    Hi amir sherafatian,

    You can avoid a for-loop in your remove method using the IObjectScope Remove method: it accepts also IEnumerable parameters as an input. You can do the following to accomplish your goals:

    1.    Filter data with a LINQ query like:
           IList<DCDocumentAdmin> docs =
                       (from doc in scope.Extent<DCDocumentAdmin>()
                        where doc.DCGroup.GroupId == groupId
                        select doc).ToList<DCDocumentAdmin>();

    2.    Then pass the filtered collection to the IObjectScope Remove method like:
           scope.Transaction.Begin();
           scope.Remove(docs);
           scope.Transaction.Commit();

    Sincerely yours,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 29 Sep 2009 Link to this post

    hi Damyan Bogoev

    you right but for make a relation i declare a collection of DCDocumentAdmin in DCGroup as property so i have not access to DCGroup Field in DCDocumentAdmin .
    and when i fetch the special DCGroup to Remove its DCDocumentAdmin i have access to that collection of DCDocumentAdmin property ( that its name is " ArchiveAdmins ") , but this property only contain a refrence to its DCDocumentAdmin so when i use this :

    DCDocumentAdminInstance.ArchiveAdmins.Clear();
    only all refrence removed and the data was still in database;

    what is your solution

    tanks

  5. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 29 Sep 2009 Link to this post

    Hello Amir,

    Try to take a look at this thread

    It deals with the issue around Clear() that you are encountering.. one solution would be to make the collection managed and mark the children with <delete-orphans> in app.config.

    Regards

    Henrik


  6. amir sherafatian
    amir sherafatian avatar
    42 posts
    Member since:
    Aug 2009

    Posted 29 Sep 2009 Link to this post

    merci
Back to Top
DevCraft banner