SelectMany()在下面是如何工作的,它使用什么算法



所以我有一个不那么特殊的方法

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();
        }
    }

我的问题是,当使用linq时,如何仍然将新的列表元素标识为原始集合的一部分,或者这只是来自实体框架对象跟踪器的东西?

Entity Framework跟踪它检索的所有对象和集合,因此当您调用context.SaveChanges()时(我认为这就是uow.Commit()方法中发生的情况),它已经知道要检查什么了。

希望能有所帮助。

最新更新