Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest search list of objects by single string
    text
    copied!<p>I am doing a web application where I have a textbox for searching through a list of objects.</p> <p>The objects is like :</p> <pre><code> public class Project : IDbProject { public string ProjectName { get; set; } public string Country { get; set; } public string Customer { get; set; } public DateTime UploadDate { get; set; } public DateTime CreateDate { get; set; } public string CreatedBy { get; set; } } </code></pre> <p>, and the list is a <code>IList&lt;IProject&gt;</code>. My search/filtering is going trough the list, and compares with each string in the object like:</p> <pre><code> public IList&lt;IProject&gt; GetSearchedProjects(string searchString) { foreach (var proj in _projects) { if (InputStartWithSearch(proj.ProjectName, searchString) &amp;&amp; !searchProjects.Contains(proj)) { searchProjects.Add(proj); continue; } if (InputStartWithSearch(proj.Country, searchString) &amp;&amp; !searchProjects.Contains(proj)) { searchProjects.Add(proj); continue; } if (InputStartWithSearch(proj.CreatedBy, searchString) &amp;&amp; !searchProjects.Contains(proj)) { searchProjects.Add(proj); continue; } if (InputStartWithSearch(proj.ProjectState, searchString) &amp;&amp; !searchProjects.Contains(proj)) { searchProjects.Add(proj); continue; } if (IsStringDate(searchString)) if (IsDatesEqual(proj.CreateDate, searchString) &amp;&amp; !searchProjects.Contains(proj)) { searchProjects.Add(proj); } } return searchProjects; } return _projects; } </code></pre> <p>As you can see, I have made a lot of private methods that will check the search string with the object's string / date / whatever. And it all works just fine.</p> <p>But is there a better/faster way to search in a list of objects by a single string?</p> <p><strong>EDIT</strong></p> <p>The methods <code>InputStartWithSearch</code> and <code>IsStringDate</code> is private methods, where I check if the input string starts with the project's data. So if the CreatedBy was "Matthi Smith Junior", and I searched for "Matthi Junior", "Matthi Smith", "Smith Junior", etc., it will add it. </p> <p>The IsStringDate is checking if the search string is a equal to a Datetime format. So it contains an array of formats, and check if the search string is in that format.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload