Solved

how to FindAll in a generic list ?

Posted on 2008-09-29
7
628 Views
Last Modified: 2012-05-05
Error      5      Argument '1': cannot convert from 'anonymous method' to 'System.Predicate<People>'

 i have a search page which have 5 dropdownlist and two text-box and i have a gridview so basically it's a search page and user can select any of those ddl or text box and click on search to populate the gridview.

i have come-up with the below code and just thinking what is the best way or pattern to achieve what i want to achieve?

here is i want: FINDALL in the custom business object... any thoughts?

ps: do i need to add pass 7 parameters? because i have 7 controls on the page

public static List<People> GetData(int id)

{

List<People> peoples = new List<People>();

 

peoples.Add(new People(1, "Bart", "Long", "Mower", 10.00M, 18));

peoples.Add(new People(2, "Al", "Short", "Garbage Man", 18.00M, 23));

peoples.Add(new People(3, "Phil", "Short", "Construction", 23.50M, 22));

.....

....  
 

if (id != 0) //new code

{

List<People> peoples1 = new List<People>();

People people = new People();

people = peoples.Find(delegate(People p) { return p.Key == id; }); //error:

peoples1.Add(people);

return peoples1;

}
 

return peoples;

}
 
 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName ="DataAccess" CacheDuration="300" CacheExpirationPolicy="Sliding">

<SelectParameters>

<asp:ControlParameter ControlID="TextBox1" ConvertEmptyStringToNull="true" PropertyName="Text" Name="id" Direction="Input" Type="int32" />

</SelectParameters>

</asp:ObjectDataSource>

Open in new window

0
Comment
Question by:nisarkhan
  • 4
  • 3
7 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601498
You can utilize the functions of System.Array like FindAll and pass in different Predicate delegate based on which value you want to search on.

So basically, if I send name "Bart", I would need a function that takes in a People object or String and correctly returns true or false based on existing People objects in List then same for other fields.  Using the Predicate method allows you to use the same array/list and do different matches based on what you pass to call of Array.FindAll.

Here is an example:
http://blogs.msdn.com/rfarber/archive/2006/07/28/681578.aspx

Here is official MSDN on List.FindAll since technically you can do right from List itself:
http://msdn.microsoft.com/en-us/library/fh1w7y8z(VS.80).aspx

Here is the one for Array.FindAll:
http://msdn.microsoft.com/en-us/library/1kkxfxdd(VS.80).aspx
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601547
And I apologize, must be a lack of coffee as I see you already implemented the Predicate approach with Find and had a question on changing approach due to the 7 controls.

You can take what I was saying about the different Predicate match methods I think to get it to work as the Predicate must take exactly one parameter of the same type as your array, so you would just use different Predicate methods to implement your different match logic based on different fields.
0
 
LVL 7

Author Comment

by:nisarkhan
ID: 22601639
can you show me the example of how i will be implemeting?
thanks
0
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.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22601775
Think you just need this:

{The say Name is another property of People, you can have a GetDataByName(String name)}
if (id != 0) //new code

{

   return peoples.FindAll

   (

        delegate(People p) 

        { 

            return (p.Key == id); // your original code looks right

            // added paranthesis only for clarity...

        }

    );

}
 

// ...
 

   return peoples.FindAll

   (

        delegate(People p) 

        { 

            return (String.Compare(p.Name, name); 

        }

    );

Open in new window

0
 
LVL 7

Author Comment

by:nisarkhan
ID: 22602007
i'm still not sure about the above code

let say i have parameters
id,
name
date

if all three parameters passed then how your code will handle ?

i see that the above code you doing "return after every parameter per say (id, name)

make sense?
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22602033
So you mean you want to do something like this:
{I am showing as separate if's here to make the point you can construct different segments of logic -- if your test is simple you could use && as if one of the three don't match then there isn't a match -- if you want to have one or the three match you can use || -- or any combination you want}
return peoples.FindAll

   (

        delegate(People p) 

        { 

            if (String.Compare(p.Name, name) <> 0)

            {

                return false;

            }
 

            if (p.Key != id)

            {

                return false;

            }

         

            if (p.Date == date)

            {

                return false;

            }
 

            return true;

        }

    );

Open in new window

0
 
LVL 7

Author Comment

by:nisarkhan
ID: 22602072
interesting let me try and get back to you.
thanks for reply.
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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

16 Experts available now in Live!

Get 1:1 Help Now