Solved

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

Posted on 2011-09-29
7
903 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

13 Experts available now in Live!

Get 1:1 Help Now