Solved

How do I use LinkedList FindLast with my own type?

Posted on 2011-09-28
4
339 Views
Last Modified: 2013-12-17
I am using a linked list and want to start removing items from the end of the list based on a property of the type that the list is holding.  See code snippet.

class MyType {
public long MyTypeCategoryId
}

LinkedList<MyType> list = new LinkedList<MyType>(...)

//Find where MyTypeCategoryId == 7
LinkedListNode<MyType> lln = list.FindLast(???);

Open in new window

0
Comment
Question by:Bruce
  • 3
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
Comment Utility
I don't believe you'll be able to use FindLast because it expects a reference to an existing car object--at least one that exists in the list. Is it possible to use Linq (.NET 3.5+)? If so, you could do something like:

LinkedList<Car> myList = new LinkedList<Car>();

myList.AddFirst(new Car() { Make = "Honda", Model = "Civic" });
myList.AddFirst(new Car() { Make = "Toyata", Model = "Tundra" });

Car theCar = myList.Where(c => c.Make == "Toyata").LastOrDefault();

if (theCar != null)
{
    // found the car!
}

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
P.S.

You would need the following namespace import:

using System.Linq;

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Correction to line 6 in my first post:

Car theCar = myList.LastOrDefault(c => c.Make == "Toyota");

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 150 total points
Comment Utility
You need to create a class that implements the IComparer interface. You can then pass that class to the Sort method of the collection:

.Sort(New DocumentationComparer())

Unfortunately, I do not have any code in C# to provide. Most of my advanced work is done in VB and the couple of such classes I have are quite involved because they can sort on many different criterias.

There is a quite simple C# example of how to implement IComparer if you lookup IComparer Interface in the documentation.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now