Solved

How do I use LinkedList FindLast with my own type?

Posted on 2011-09-28
4
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 350 total points
ID: 36720082
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 75

Expert Comment

by:käµfm³d 👽
ID: 36720092
P.S.

You would need the following namespace import:

using System.Linq;

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36720104
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
ID: 36720131
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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