Solved

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

Posted on 2011-09-29
7
924 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now