Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I use LinkedList FindLast with my own type?

Posted on 2011-09-28
4
Medium Priority
?
351 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 75

Accepted Solution

by:
käµfm³d   👽 earned 1400 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 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 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