Generics Find Method ?? Predicate

Morning All...

I am using Generics to handle my list of objects.  I have never used generics before so am kinda bumbling along here!!!

I have my custom access layer to perform certain custom actions i want but have now hit a wall :(

I want to use the Find method to extract one Item from the Collection searching by id (property of my collection)
It is asking for a Predicate? I have no idea what that is or how to use it (i dont have a great understanding of delegates and have hardly used them)

collection class contructor...
public CustomDS(int id, string name, string secondaryname)
    {
        this._id = id;
        this._name = name;
        this._secondaryname = secondaryname;
    }

So my list has many of these objects and i want to do this.....
CustomDS GetSingle(int id, ref List<CustomDS> list) {
CustomDS item = list.Find(/*search by CustomDS.ID in here some how*/);
}

Can someone point me in the right direction for this?  I have had a look around and dont really understand what i am reading :(

Thanks
o
LVL 7
osiris247Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
int searchID;

CustomDS GetSingle(int id, List<CustomDS> list)
{
    searchID = id;    // keep ID in class member for using in HasID function
    CustomDS item = list.Find(HasID);
    return item;
}

// This function is called for every list element. If function returns true,
// this element is returned by Find function.
bool HasID(CustomDS c)
{
    return ( c.Id == searchID );
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
osiris247Author Commented:
Thanks Alex...worked a treat.  Now i understand what to do i can stop using the index to remove as the selectedindex property of my gridview seems a little flakey and gives 3 when there is only one row

One thing that concerns me a little.  I have DAL methods as static so my global int has to be static also.  Do you foresee any problems with this?

Now on with the Sort!

Thanks
o
0
AlexFMCommented:
I don't see problem, all these functions and variables can be static. For example, MSDN topic List<T>.Find has sample code which works with static functions.
0
osiris247Author Commented:
Thanks Alex.

Just about to open a question about the sorting my collection :(  ....please take a look if you get time.

o
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.