0

I want to do a filter for data without changing variable or declare new one.

I am looking for result like this - I know the where doesn't update in original var

classes.Where(i => classStudent.ListEtab.Contains(i.schoolId))
       .ToList()
       .ForEach(cc => cc.Disabled = false);

I try to create a new variable and make the change in it, but I want to return the same variable classes.

get
{
    classes.ToList().ForEach(cc => cc.Disabled = true);
    classes.Where(i => classStudent.ListEtab.Contains(i.schoolId))
           .ToList()
           .ForEach(cc => cc.Disabled = false);

    return classes;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • My boss did a really cheap but expensive thing, but have you tried serializing to json/raw and then deserializing the thing. Always ends up as a different clone? – Monofuse Jan 13 '23 at 22:25
  • Ugh... just [clone](https://stackoverflow.com/questions/2289420/what-is-the-method-memberwiseclone-doing) it... – TGnat Jan 13 '23 at 22:54

1 Answers1

0

It's not clear from your question, but Filtering does not require changing data state.

If a class has a Disabled state, and that state can be persisted, meaning stored in the Database between calls then the entity and table can have a Disabled field to reflect that.

To get all classes:

var allClasses = _context.Classes.ToList();

To get all enabled classes:

var enabledClasses = _context.Classes.Where(x => !x.Disabled).ToList();

Now if you want to filter out from all classes for a particular set of school IDs:

var selectedClasses = _context.Classes.Where(x => classStudent.ListEtab.Contains(x => x.SchoolId).ToList();

This doesn't alter any data state, it just creates a set of references for particular classes that match your criteria.

Now if you don't particularly want to persist this "disabled" state, but alter it for the duration that your view, or code logic is running, then I recommend rather than using Entities, you use a View Model containing the fields about the class that you care about, then this view model can have the Disabled state which you can manipulate without worrying about it being persisted to the database. Alternatively entities can also have [NotMapped] properties that aren't persisted in the tables, but I generally do not recommend these because they require special attention as they cannot be used in query expressions that would be translated to SQL.

In this case the Class entity and table can either be set up with a more permanent Disabled state to consider, or you can remove it all-together.

public class ClassViewModel
{
     public int ClassId { get; set; }
     // other class related fields...

     public bool Disabled { get; set;}
}

Now when you read the classes, rather than reading entities which will be tracking changes for fields you don't want to store or change in the database, we project to ViewModels that the views etc. can use however they see fit:

var classes = _context.Classes
    .Select(x => new ClassViewModel
    {
        ClassId = x.ClassId,
        // ... other fields....

        Disabled = !classStudent.ListEtabl.Contains(x.SchoolId)
    }).ToList();

If the class does have a persisted Disabled state that you want to consider (if the DB says a class is disabled it should remain disabled even if the school ID is in the list for example):

var classes = _context.Classes
    .Select(x => new ClassViewModel
    {
        ClassId = x.ClassId,
        // ... other fields....

        Disabled = x.Disabled || !classStudent.ListEtabl.Contains(x.SchoolId)
    }).ToList();

The key point is that these view models can be consumed, altered, etc. however you want without altering the underlying entities. If you make alterations to the view model that you later want to save to the entities and underlying tables, load the applicable entity(ies) by ID and copy the updated values across and call SaveChanges.

Steve Py
  • 26,149
  • 3
  • 25
  • 43