Changing the value works with loading look, but not lazing loading in linq and ef

This is not a "problem" question, but "why is this happening."

var chapters = story.Chapters.Select(
                    ch => new ChapterDisplayViewModel { 
                                   Id = ch.Id,
                                   Number = ch.Number});

First I want to get some data. story is a type of entity type Storyand is related to One To Many s Chapter. I want to change some of the data in the collection of chapters that I have, so I'm writing some kind of condition, if I change the value

if(chapters.Any(c => c.Number == chapterNum))
   chapters.Where(c => c.Number == chapterNum).Single().IsSelected = true;

and then I send the data to the view, but the problem is this:

nothing has changed due to lazy loading, the change I made was not caused even after sending the data to the view, why? did i make an account and shouldn't pass data to the view causing it?

, , ToList

var chapters = story.Chapters.Select(
                    ch => new ChapterDisplayViewModel { 
                                   Id = ch.Id,
                                   Number = ch.Number}).ToList();

+3
1

, Chapters Story, , Chapters . , , , ...

var chapters = story.Chapters.Select(
               ch => new ChapterDisplayViewModel { 
                               Id = ch.Id,
                               Number = ch.Number});

... , Chapter ( ChapterDisplayViewModel ). . ...

if (chapters.Any(c => c.Number == chapterNum))
    chapters.Where(c => c.Number == chapterNum).Single().IsSelected = true;

... Chapters. .

, Single ChapterDisplayViewModel, : - new ChapterDisplayViewModel. :

var viewModel1 = chapters.Where(c => c.Number == chapterNum).Single();
var viewModel2 = chapters.Where(c => c.Number == chapterNum).Single();

bool sameObjects = object.ReferenceEquals(viewModel1, viewModel2);

sameObjects false, , Single ViewModel, , .

ToList , ViewModels ViewModels , Single , . sameObjects true.

, ToList IsSelected , , . ToList . , .

+2

All Articles