Change state of List<T> inside extension method

Avais

How do I change the state of List which is part of view model in extension method so the view model reflects this change without having to re assign the value

Code:

//ViewModel:

//This line should modify the change internally wihtout having to reassign like

// ..Selected =model.CheckBoxList.Selected.RemoveWhere(c => c.SelectedValue == null)

  model.CheckBoxList.Selected.RemoveWhere(c => c.SelectedValue == null) 

Extension Method :

 public static IEnumerable<T> RemoveWhere<T>(this IEnumerable<T> source, Predicate<T> predicate)

{

            var x = source.ToList();
            x.RemoveAt(1);
            source = x;
            return source;

}

Update:

public class CheckBoxList
    {
        public CheckBoxList()
        {
            //Selected = new List<CheckBoxListViewModel>();
        }

        [XmlElement("SelectedItem")]
        public List<CheckBoxListViewModel> Selected { get; set; }
    }

    [XmlRoot(ElementName = "MyClass")]
    public class CheckBoxListViewModel
    {
        public string SelectedValue { get; set; }
        public string SelectedDescription { get; set; }
    }
SLaks

Assign a parameter has no effect on the expression you pass to the method.

You instead want to mutate the existing instance. To do that, you need to accept a mutable type; namely, List<T>.

List<T> already has a RemoveAll() function which does exactly that, so you don't need to do anything at all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't use == in LINQ Extension Method

From Dev

Extension method to check if an object is sub class of T

From Dev

How to use Extension method inside a LINQ statement?

From Dev

How to pass an async method inside the Observable.Do extension method?

From Dev

Extension Method for List<InterfaceType> is not recognized

From Dev

Nested Func<T, object> in a Generic Extension Method

From Dev

Change state of angular app from resolve method of state provider

From Dev

From Linq To List Using Extension Method

From Dev

IQueryable extension method to generate a SelectListItem list

From Dev

Getting list of types that are effected by an extension method in cqlinq

From Dev

is it possible to write an extension method only for List<T> where T is a class that inherits from class K

From Dev

Extension Method for a List

From Dev

Method with generic type parameter inside Swift extension

From Dev

Access State inside of mapDispatchToProps method

From Dev

State doesn't change in react

From Dev

How to pass an async method inside the Observable.Do extension method?

From Dev

Extension Method for List<InterfaceType> is not recognized

From Dev

Check via reflection if specific extension method is used inside the method

From Dev

Add extension method to List

From Dev

Celery update_state inside class method

From Dev

Getting list of types that are effected by an extension method in cqlinq

From Dev

List<T> to XML string Extension Method

From Dev

ForEach List<T> Extension method - Value Or Reference Type?

From Dev

Why doesn't assigning a new value to a list element inside an if block (which is inside a for loop) not change the actual list itself?

From Dev

Keeping State of an array inside a recursive method

From Dev

iOS Extension: can't change icon

From Dev

C# List<object> unable to use extension method of List<T>

From Dev

extension method for List<T> with both constraints class and struct

From Dev

The state in reactjs doesn't change

Related Related

  1. 1

    Can't use == in LINQ Extension Method

  2. 2

    Extension method to check if an object is sub class of T

  3. 3

    How to use Extension method inside a LINQ statement?

  4. 4

    How to pass an async method inside the Observable.Do extension method?

  5. 5

    Extension Method for List<InterfaceType> is not recognized

  6. 6

    Nested Func<T, object> in a Generic Extension Method

  7. 7

    Change state of angular app from resolve method of state provider

  8. 8

    From Linq To List Using Extension Method

  9. 9

    IQueryable extension method to generate a SelectListItem list

  10. 10

    Getting list of types that are effected by an extension method in cqlinq

  11. 11

    is it possible to write an extension method only for List<T> where T is a class that inherits from class K

  12. 12

    Extension Method for a List

  13. 13

    Method with generic type parameter inside Swift extension

  14. 14

    Access State inside of mapDispatchToProps method

  15. 15

    State doesn't change in react

  16. 16

    How to pass an async method inside the Observable.Do extension method?

  17. 17

    Extension Method for List<InterfaceType> is not recognized

  18. 18

    Check via reflection if specific extension method is used inside the method

  19. 19

    Add extension method to List

  20. 20

    Celery update_state inside class method

  21. 21

    Getting list of types that are effected by an extension method in cqlinq

  22. 22

    List<T> to XML string Extension Method

  23. 23

    ForEach List<T> Extension method - Value Or Reference Type?

  24. 24

    Why doesn't assigning a new value to a list element inside an if block (which is inside a for loop) not change the actual list itself?

  25. 25

    Keeping State of an array inside a recursive method

  26. 26

    iOS Extension: can't change icon

  27. 27

    C# List<object> unable to use extension method of List<T>

  28. 28

    extension method for List<T> with both constraints class and struct

  29. 29

    The state in reactjs doesn't change

HotTag

Archive