none
LIST.FindALL parameters

    Question

  •  

    HI all

     i have populated List with rows from datattable and it contains data like 

    fd20,fd21 ,kp12

     

    now what i want ..

    i want to populate 2nd list with those values where it stars with fd...

    as we used in sqlsrver

     

    ModelName like '%FD%'"

     

    so how i can u find all these itmes in list 1 and put in list 2

    Monday, March 10, 2008 9:30 AM

Answers

  • Hi!

     

    If using Visual Studio 2008 LINQ will help you to do this:

    Code Snippet

     

    List<string> list = new List<string>();

    list.Add("fd1");

    list.Add("fd2");

    list.Add("fd3");

    list.Add("sg");

    var q = from s in list

    where s.StartsWith("fd")

    select s;

    List<string> list2 = new List<string>(q);

     

     

    If using Visual Studio 2005 you have to use a Predicate delegate:

     

    Code Snippet

    static void Main(string[] args)

    {

    Predicate<string> test = delegate(string s) { return s.StartsWith("fd"); };

    List<string> list = new List<string>();

    list.Add("fd1");

    list.Add("fd2");

    list.Add("fd3");

    list.Add("sg");

    List<string> list2= list.FindAll(test);

    }

     

     

    Hope this helps ;-)
    Monday, March 10, 2008 9:45 AM
  • Note that the LINQ example above doesn't quite do the same thing - it doesn't invoke FindAll at all, but uses Enumerable.Where etc.

    Also, in both cases the example is the "long way"; you can use C# 3 lambda expressions (or C# 2 anonymous methods) to call FindAll much more concisely:

     

    C# 2:

    Code Snippet

    List<string> list2 = list.FindAll(delegate(string s) {

        return s.StartsWith("fd");

    });

     

    C# 3;

    Code Snippet

    List<string> list3 = list.FindAll(s => s.StartsWith("fd"));

     

    Monday, March 10, 2008 11:35 AM
  • well thanks bro for ur seggestion

    actually i wanted to avoid db trip again again for this purpose i used xml file and put the data into XML file and update the XML file only when db is changed....

    so i m getting this information from the XML file into datatable using ReadXml method...

    well thanks to all of u

    Tuesday, March 11, 2008 1:19 PM

All replies

  • Hi!

     

    If using Visual Studio 2008 LINQ will help you to do this:

    Code Snippet

     

    List<string> list = new List<string>();

    list.Add("fd1");

    list.Add("fd2");

    list.Add("fd3");

    list.Add("sg");

    var q = from s in list

    where s.StartsWith("fd")

    select s;

    List<string> list2 = new List<string>(q);

     

     

    If using Visual Studio 2005 you have to use a Predicate delegate:

     

    Code Snippet

    static void Main(string[] args)

    {

    Predicate<string> test = delegate(string s) { return s.StartsWith("fd"); };

    List<string> list = new List<string>();

    list.Add("fd1");

    list.Add("fd2");

    list.Add("fd3");

    list.Add("sg");

    List<string> list2= list.FindAll(test);

    }

     

     

    Hope this helps ;-)
    Monday, March 10, 2008 9:45 AM
  • Note that the LINQ example above doesn't quite do the same thing - it doesn't invoke FindAll at all, but uses Enumerable.Where etc.

    Also, in both cases the example is the "long way"; you can use C# 3 lambda expressions (or C# 2 anonymous methods) to call FindAll much more concisely:

     

    C# 2:

    Code Snippet

    List<string> list2 = list.FindAll(delegate(string s) {

        return s.StartsWith("fd");

    });

     

    C# 3;

    Code Snippet

    List<string> list3 = list.FindAll(s => s.StartsWith("fd"));

     

    Monday, March 10, 2008 11:35 AM
  • Hi Marc,

    you are really true Smile

     

    I showed up the LINQ example, because although mian ghous talked about List.FindAll in his message, i just wanted to show another way to do the same.

     

    Anyway, your example with lambda expressions is really great! i thnik lamda expressions is one of the best new features of C# 3.0

     

    Greetings ;-)

    Monday, March 10, 2008 12:47 PM
  • I would suggest not searching the list for values if that list contains the result of a database query.  I would suggest executing another database query with a revised where clause.

     

    You can do what you wan with the List<T>.FindAll(Predicate<T>), but you'll also have to employ regular expressions.

     

    Monday, March 10, 2008 2:59 PM
    Moderator
  • well thanks bro for ur seggestion

    actually i wanted to avoid db trip again again for this purpose i used xml file and put the data into XML file and update the XML file only when db is changed....

    so i m getting this information from the XML file into datatable using ReadXml method...

    well thanks to all of u

    Tuesday, March 11, 2008 1:19 PM