Solved

How do I get the index of an item in an IList based of that Item...?

Posted on 2011-09-29
7
1,021 Views
Last Modified: 2013-12-17
I am trying to remove a specific Item from an IList the is found via Linq.  

How do I get the index from an item itself?

See my code below
IList<MyType> myList = new List<MyType>();
MyType myTItem = myList.OrderByDescending(x => x.YearOfService).Where(y = y.YearOfService < 2001).Last();
long indx = ??? //index of myTItem
myList.RemoveAt(indx);

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
  • 4
  • 2
7 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36816287
Try:
IList<MyType> myList = new List<MyType>();
MyType myTItem = myList.OrderByDescending(x => x.YearOfService).Where(y = y.YearOfService < 2001).Last();
long indx = myList.IndexOf(myTItem); //index of myTItem
myList.RemoveAt(indx);

Open in new window

Also:
IList<MyType> myList = new List<MyType>();
MyType myTItem = myList.OrderByDescending(x => x.YearOfService).Where(y = y.YearOfService < 2001).Last();
myList.Remove(myTItem);

Open in new window

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36816297
The index must be a int (first example, line #3)
0
 
LVL 1

Author Comment

by:Bruce
ID: 36816387
"Try:"
I don't have IndexOf on my IList.

"Also:"
myList.Remove(myTItem) errors out.
Argument type 'MyType' is not assignable to parameter type 'System.Predicate<MyType>'
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36816435
mmm we are missing something... I list has that method:
http://msdn.microsoft.com/en-us/library/3w0148af(v=VS.80).aspx
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 250 total points
ID: 36816491
Try this example:
void Test()
{
    System.Collections.Generic.IList<MyType2> myList = new System.Collections.Generic.List<MyType2>();
    myList.Add(new MyType2(2005));
    myList.Add(new MyType2(2000));
    myList.Add(new MyType2(2001));
    myList.Add(new MyType2(1999));
    MyType2 myTItem = myList.OrderByDescending(x => x.YearOfService).Where(y => y.YearOfService < 2001).Last();
    int indx = myList.IndexOf(myTItem);
    myList.RemoveAt(indx);
}

class MyType2
{
    public int YearOfService { get; set; }
    public MyType2(int year)
    {
        YearOfService = year;
    }
}

Open in new window

0
 
LVL 4

Assisted Solution

by:theHollow
theHollow earned 250 total points
ID: 36816545
Hi
Is there a particular reason why you need to go via the item's index?

I tried yv989c's example, and it works fine.
You may also remove the item directly by doing this:

(...)
MyType2 myTItem = myList.OrderByDescending(x => x.YearOfService).Where(y => y.YearOfService < 2001).Last();
myList.Remove(myTItem );
(...)

Open in new window

0
 
LVL 1

Author Comment

by:Bruce
ID: 36817679
Sorry, I didn't represent my example well...

I was using a class and interface that were created by another developer.  The class inherited from the interface and the interface from IEnumerable.  I was under the impression that it was IList and built my example accordingly.

I've got it working now.  

I'm gladly giving the points.  Thanks for the help.    
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

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