Solved

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

Posted on 2011-09-29
7
1,080 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

635 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