Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I use LinkedList FindLast with my own type?

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

Technology Partners: 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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