Results 1 to 3 of 3

Thread: Searching value containing any string (or part of) in a string array

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Searching value containing any string (or part of) in a string array

    I will have a search string but need to search by each individual string in it, so I'm creating an array:
    string[] criteria = param.sSearch.Split(' ');

    Now the first issue is that say "Jack ", will give you two items, the latter be a space.

    Then I search on a collection of say "Person" objects, where I overriden the "ToString" method to give you someting like "FirstName LastName EmailAddress Employer Andsoon"

    I want to return all items in the collection where any string in the criteria array match (even partly) any value in the object's .tostring value.

    not sure if it make sense, but for what it matter the following does not work as I hope:
    Code:
    IList<CompanyMember> allMembers = CurrentCompany.Members.ToList();
    IEnumerable<CompanyMember> filteredMembers = allMembers;
    if (!string.IsNullOrEmpty(param.sSearch))
    {
        string[] criteria = param.sSearch.Split(' ');
    
        filteredMembers = from xin allMembers
                            where criteria.All(val => x.ToString().Contains(val))
                            select x;
    }


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Searching value containing any string (or part of) in a string array

    That explanation is not very clear I'm afraid. First of all, this:
    Code:
    string[] criteria = param.sSearch.Split(' ');
    will indeed give you an array with two elements but the second will be an empty string, not a string containing a space. You also don't specifically indicate whether that is actually what you want or not. If you don't want that then use one of the Split overloads that lets you exclude empty results. Regardless, I think that a full and clear explanation with an example or two might be in order.

  3. #3

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Searching value containing any string (or part of) in a string array

    Ok
    I'm not sure if you used jQuery.DataTables before? It have a search function (happening client-side by default) where you can type in parts of the value in one column, part of value of another column, and it will filter down to say where any row have one column where the value CONTAIN this or that. See here for a demo.

    I'm using that, but my searching, sorting, etc happen server side. Involved a bit more work.

    So to stick to the behaviour of the client side searching, I overridden the tostring method of the objects being listed to be one long string of I want to search on (so for instance, tostring will result in "Jim Chimney VBForums Married Ford Focus Software Developer" for a given row object)

    Then when you start typing in the search box I want to break whatever you typed into seperate strings (array) and ask "Give me all rows (object) where any part of the tostring contains any of the items in the array)

    So if you type "Jim" or "Jim Ford" or even "Chim Fo Soft", it will return your record an any other record where any part of that record contain any of the given search words.

    I'm not sure if that make any sense?

    1) Take the search string "Chim Fo Soft" and break into an array : { "Chim", "Fo", "Soft" } - I don't want "Chim " to result in a array with two items though.
    2) The match the list of object's tostring values to any of the items in the above. So, so I'm also in there and for instance tostring include "Softtard", my record will also be included (Soft = Softard)

    Does that explain it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width