AddRange opposite

Posts   
 
    
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 22-Aug-2010 05:30:03   

Hi, AddRange adds the range of objects passes into an entity collection. is there an opposite to this like RemoveRange?

i have 200 students and i've made the schedule of 180 students. Now i would like to find the ones (remaining 20) that i havent made a schedule yet.

any ideas? thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Aug-2010 22:46:26   

Do you have the collection(s) in memory or you want to perform a query that returns the remaining 20 entities?

What do you have so far?

David Elizondo | LLBLGen Support Team
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 23-Aug-2010 00:25:19   

daelmo wrote:

Do you have the collection(s) in memory or you want to perform a query that returns the remaining 20 entities?

What do you have so far?

if there is a way to perform a query that will return the remaining 20, that would be great. i dont want to go through each entity of the second collection and drop it if it exists in the first collection. having 600 students and 1 student with no schedule means 599 compare/drops.

would it be possible to return that 1 student?

thank you

students are held in student table and enrollment data is kept in enrollment table with studentid, classid, and semesterid fields. so finding students that are enrolled in a class is easy. lets say i have the two collections, <all students> and <students with enrollment> data. how can i get the students with no enrollment data out of this two (without going through all entities of <students with enrollment> collection)?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2010 09:14:47   

To do this in memory you either have to loop on the all students collection to pick those who are not in the enrollement collection. i.e. 2 nested loops.

Or use Linq2Objects as follows:

var q = from s in students
                     where !(from s2 in enrolledStudents
                             select s2.Id)
                                .Contains(s.Id)
                     select s;
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 23-Aug-2010 09:16:40   

Walaa wrote:

To do this in memory you either have to loop on the all students collection to pick those who are not in the enrollement collection. i.e. 2 nested loops.

Or use Linq2Objects as follows:

var q = from s in students
                     where !(from s2 in enrolledStudents
                             select s2.Id)
                                .Contains(s.Id)
                     select s;

never used link2objects before. is this all i need? that will return a what? a collection?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2010 09:41:10   

You will have to use the following line to get the collection:

q.ToList();

This will return a List<Type> of the same Type of the students collection.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 23-Aug-2010 10:25:38   

Linq to objects also simply loops over your collections. It's not possible to find elements with an algorithm less than O(n), you always have to look at each element at least once.

You could built it into the schedule create routine however. I.e: add all processed elements to a list, and then traverse that list again to remove the elements from the list to process elements.

This isn't efficient, as it will do a linear search. If you want to do this fast, first store the elements to process in a HashSet<T>, which has ~O(1) (amortized, so give or take) lookup capabilities. Removing the element from the list of elements already processed is very quick.

Frans Bouma | Lead developer LLBLGen Pro