Question

Here is my situation. I have 2 list of the same type. Imagine the names like these. FullList and ElementsRemoved. So in order to avoid the database roundtrip, anytime I delete an element from the fulllist I added to the list of ElementsRemoved in case of regret's user so he can revert the deletion.

I was thinking to loop inside my ElementsRemoved to insert them again into the FullList from where initially were removed.

There is any way to do this as simple with List Methods.

Something like

FullList.Insert, Add, ..... (x => 

in order to reduce line code and optimized?

Was it helpful?

Solution 2

In your front end make a class that holds a bool and your object:

public class DelPair<T>{
   public bool IsDeleted{get;set;}
   public T Item{get;set;}
}

Now instead of using a list of objects use a list of DelPair<YourClass> and set IsDeleted=true when deleting.

This pattern will also allow you to track other things, such as IsModified if it comes to that.

Based on OP comment that he's using an ENTITY class and needs it to function as such:

One option is to make your DelPair class inherit ENTITY. Another may be to put implicit casting operator:

...
   // not exactly sure about the signature, trial/error should do :)
   public static implicit operator T(DelPair<T> pair) 
   {
      return pair.Item;
   }

OTHER TIPS

Instead of deleting the item from your database consider using a flag in the table.

For example consider this entities table (written in TSQL):

CREATE TABLE Entity
(
    Id INT IDENTITY PRIMARY KEY
   ,Name NVARCHAR(20) NOT NULL
   ,IsDelete BIT NOT NULL DEFAULT 0
);

This way you can set the IsDelete bit when the user deletes the entity which will prevent the data from being lost. The data can be pruned on a job in the off hours.

The would lead to only needing one list instead of keeping track of two lists.

public class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDelete { get; set; }
}

public static void UndoDelete(IEnumerable<Entity> fullList, int[] removedIds)
{
    foreach(var entity in fullList.Where(e => removedIds.Contains(e.Id)))
    {
        entity.IsDelete = false;
    }
}

In case you cannot modify your application.

You can simply add the entities back in.

See List(T).AddRange

var entitiesToAdd = new[] { 2, 3, 4 };

var entitiesToInsert = ElementsRemoved.Where(e => entitiesToAdd.Contains(e.Id)); 
FullList.AddRange(entitiesToInsert);

Suppose you have an element having a field id which uniquely identifies it.

class Element{public int id;}

In that case you can do this

FullList.Add(ElementsRemoved.FirstOrDefault(e=>e.id==id));

In case you want to add all elements use AddRange

FullList.AddRange(ElementsRemoved);

You can use the AddRange method

FullList.AddRange(ElementsRemoved);

But consider doing this

public class YourClass
{ 
     public string AnyValue{get;set;}
     public bool IsDeleted{get;set;}
}

And you have list like this List < YourClass> FullList. Now whenever user removes any item you just set the

IsDeleted = true 

of the item that is removed. This will help you in keeping just one list and adding removing from the list

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top