This is a migrated thread and some comments may be shown as answers.

Clear All Childs With 1 Query

4 Answers 72 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
amir sherafatian
Top achievements
Rank 1
amir sherafatian asked on 23 Sep 2009, 10:54 AM
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

4 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 24 Sep 2009, 03:44 PM
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.
0
amir sherafatian
Top achievements
Rank 1
answered on 29 Sep 2009, 08:08 AM
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

0
Accepted
IT-Als
Top achievements
Rank 1
answered on 29 Sep 2009, 09:17 AM
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


0
amir sherafatian
Top achievements
Rank 1
answered on 29 Sep 2009, 10:11 AM
merci
Tags
Databases and Data Types
Asked by
amir sherafatian
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
amir sherafatian
Top achievements
Rank 1
IT-Als
Top achievements
Rank 1
Share this question
or