简体   繁体   中英

How does SelectMany() work underneath, what algorithm does it use?

So i have this not so special method

public void FlagVoyageAsRemoved(int voyageId)
    {
        using (UnitOfWork uw = new UnitOfWork())
        {
            Voyage voyage = uw.VoyageRepository.FindSingle(v => v.VoyageId == voyageId,  
new string[] { "VoyageUsers.Costs" });
            List<Cost> userCosts = voyage.VoyageUsers.SelectMany(vu => vu.Costs).ToList();
//altough i am putting my items in a new list meaning its a new memory adress, the object tracker can still see them as part of the original collection, how come?
            costBl.FlagCostsAsDeleted(userCosts);
// these methods just change a proprety in each element of the collection, nothing more.
            costBl.FlagCostsAsDeleted(voyage.Costs);
            vUserBl.FlagVoyageUsersAsDeleted(voyage.VoyageUsers);
            voyage.HasDeleteFlag = true;
            uw.Commit();
        }
    }

My question is how do the new list elements can still be identified as part of the original collection, when using linq, or is this just something coming from the entity framework object tracker?

Entity Framework keeps track of all the objects and collections it retrieves, so when you call context.SaveChanges() (and that is what I think is happening in your uow.Commit() method) it already know what to check.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM