0

I have LinkedList there is Remove(item) method that get as param item.

I would like to know what I have to override to delete item by specific param?

For example I have LinkedList<MyClass>, in MyClass I have variable int index and I would like to compare by this value...

Like, I am looking for some override compare(Obj) method that I can override and compare objects when I delete items from LinkedList by value that I need...

EDIT

Method Where does not fit, because actually I have a generic LinkedList implementation and actually my LinkedList looks like LinkedList<TYPE>, and it could be any of type. Because of this I can't use where because actually I don't know the exact type

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • Look for items not equal instead of equal : LinkedList myClass = new LinkedList(); LinkedList results = (LinkedList) myClass.Where(x => x.item != 5); – jdweng Dec 10 '19 at 14:18
  • 1
    Does this answer your question? [Delete record using linQ with lambda Expression](https://stackoverflow.com/questions/29124037/delete-record-using-linq-with-lambda-expression) – Akash Shrivastava Dec 10 '19 at 14:18
  • I not sure, but isnt enough var new list = LinkedList.Where(x=>x.index ==1).ToList(); foreach (var item in list) {LinkedList.Remove(item)} – Silny ToJa Dec 10 '19 at 14:19
  • I would make my own object and overwrite remove method or I guess you can give a try to extension methods and extend LinkedList with your own method – Honza P. Dec 10 '19 at 14:19
  • you may look for this: https://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-objects , implement `IEquatable` on your `MyClass` – Dongdong Dec 10 '19 at 14:24
  • @jdweng edited my question – Sirop4ik Dec 10 '19 at 14:25
  • @AkashShrivastava edited my question – Sirop4ik Dec 10 '19 at 14:25
  • @SilnyToJa edited my question – Sirop4ik Dec 10 '19 at 14:25
  • @HonzaP. edited my question – Sirop4ik Dec 10 '19 at 14:25
  • @Dongdong edited my question – Sirop4ik Dec 10 '19 at 14:25
  • `foreach` loop will work i guess then, and then in the loop you can check for the property you're looking for? – Akash Shrivastava Dec 10 '19 at 14:27
  • if the `type` is `reference type` and the `item` you want to delete is not the `reference` of the `item`, who know how to comapre if the type does not implement IEquatable? – Dongdong Dec 10 '19 at 14:33
  • @Dongdong ok, how `Remove(item)`, knows that objects are the same? By hash and by equal methods, right? If someone call `Remove()` method, this method start to check if it has the same object in `LinkedList` and if yes, remove it. So, I would like to know how to override this method into the object correctly. – Sirop4ik Dec 10 '19 at 14:39
  • But if you dont know the type how do you know that type has your index property? – Magnus Dec 10 '19 at 15:07
  • Your "clarification" is very confusing. It would be so much easier if you would show us some sample code for the scenario, rather than just trying to describe it. – Jon Skeet Dec 10 '19 at 15:20
  • I believe the post I refered is your answer if you have no further example code or concerns. – Dongdong Dec 10 '19 at 19:53
  • Try the code I wrote. You can now check for everything in everything. Get the list of items you want to remove and remove. Hope it was helpful.)) – Arthur Grigoryan Dec 11 '19 at 07:55

2 Answers2

0

The most simple way is to set a variable that will check if each value in a linked way is the one you're looking to delete and ALSO set a variable that is one value/step behind whatever the first variable is tracking.

That way, when the main variable (the one doing the checking) spots the list value you want to remove, you set the behind variable's "next value" equal to the main variable's "next value" thus overwriting and deleting it.

Each time you increment the main variable, also increment the behind variable so that you can keep it one step behind.

DIAGRAM (left is before, right is after): https://gyazo.com/4d989b6ff6249249c9d63a17a830a8c1

Basically, just set two variables: one that is checking each linked list value to find the one you're looking to delete and one BEHIND it so that when you find the value you want to delete, you set the variable that's behind it to the value in front of the main variable thus overwriting the deleted part.

0
using System;
using System.Collections.Generic;

namespace ConsoleAppCore
{
    public static class Extension
    {
        public static List<dynamic> Where(this IEnumerable<dynamic> list, Func<dynamic, bool> func)
        {
            List<dynamic> result = new List<dynamic>();
            foreach(dynamic item in list) {
                try {
                    if (func(item))
                        result.Add(item);
                }
                catch {
                    continue;
                }
            }
            return result;
        }
    }
    class YourClass
    {
        public int x = 5;
    }
    class Program
    {
        static void Main(string[] args)
        {
            LinkedList<dynamic> list = new LinkedList<dynamic>();
            list.AddAfter(list.AddAfter(list.AddAfter(list.AddAfter(
                list.AddAfter(list.AddAfter(list.AddAfter(list.AddFirst(
                    (decimal)1), 2), (double)3), "Hello"), 5), new YourClass()), (float)7), 8);

            var newlist = list.Where(i => i == "Hello");
            // only one logical operation at a time (caused exceptions break the logic)
            newlist.AddRange(list.Where(i => i.x == 5));
            newlist.AddRange(list.Where(i => i > 5));

            foreach(var i in newlist)
                Console.WriteLine(i);
        }
    }
}

Output

Hello, ConsoleAppCore.YourClass, 7, 8

Arthur Grigoryan
  • 358
  • 3
  • 12