Solved

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

Posted on 2011-09-29
7
965 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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