Unique test for multipart key?

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 31-Oct-2008 17:54:13   

I have an entity called, PlaceEntity that has a multi-part key

CountryFK StateFK CityFK

In memory, users add new items. Is there an easy in memory Linq to LLBL call I can make to see if there are any duplicates based on this key in memory?

EntityCollection<PlaceEntity> (Adapter made)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Oct-2008 19:54:03   
// group the places
var groupPlacesQuery = from p in placesInMemory
         group p by new { p.CountryFK, p.StateFK, p.CityFK} into g
         select new { theKey = g.Key, Count = g.Count() };

// use the above query to obtain duplicates
var duplicatePlacesQuery = from p2 in placesInMemory
    join pg in groupPlacesQuery 
    on new { p2.CountryFK, p2.StateFK, p2.CityFK } equals pg.theKey                                     
    where pg.Count > 1
    select p2;

// navigate through results
foreach (PlaceEntity place in duplicatePlacesQuery)
{
    // print
    Console.WriteLine("{0}\t{1}\t{0}", place .CountryFK, place .StateFK, place .CityFK);

    // the index on the original location, in case you want to delete it.
    int indexInCollection = details.IndexOf(place);             
}
David Elizondo | LLBLGen Support Team