I am developing an app in which i have some data fetched from net into a class. Class is
public class Detail
{
public string name { get; set; }
public List<Education> education { get; set; }
public City city { get; set; }
public List<Work> work { get; set; }
}
public class Education
{
public string DegreeName { get; set; }
}
public class City
{
public string name { get; set; }
}
public class Work
{
public string name { get; set; }
}
Data is stored for a person in the above class.
Now i want to search for a string say q=" Which Manager Graduated From USA ?"
So i want it to search for the above query...
Based on how much words matched, i want to give the Name of user. So searching for person if he is a Manager Graduated From USA ? (may be less words, for search like some Director from India)
The approach i am trying to look for words like Manager in Work
and Graduate in Education
and Location for USA
I am making an array of search string
string[] qList = q.Split(' ');
and then traverse through the class. But i don't have any idea of how to (efficiently) look for data in the class.
And is my approach good enough for search or is there any better option ?
Search for a character in a string - strchr & strrchr The strchr function returns the first occurrence of a character within a string. The strrchr returns the last occurrence of a character within a string. They return a character pointer to the character found, or NULL pointer if the character is not found.
(Search String for Substring) In the C Programming Language, the strstr function searches within the string pointed to by s1 for the string pointed to by s2. It returns a pointer to the first occurrence in s1 of s2.
You can use the scanf() function to read a string. The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).
You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.
What you're developing is a human readable and writable query language. Starting with a string split may be a, well, start but imagine the search possibilities: Search for people in a city or a range of cities, search for people that worked for a top 500 company or in a certain field.
For this purpose you should develop a query language. With an easy to change and documented grammar. Take a look at ANTLR a Parser Generator that plays nice with C#/.NET.
I have somehow concern about the mechanism you are trying to implement, if user types q=" Manager Graduated From USA ?"
, mean doesn't put the word 'which' in it, so you will have to go for query language like ANTLR as suggested.
My recommendation is to give dropdowns to user, first must contain values of Work Property, 2nd must contain values of education and than a text box to enter the City.
After you pass these values to your method, use LINQ to get the data from your collection like:
var filteredResults = from result in YOURDETAILCOLLECTION
where result.city.Contains(YOURCITYTEXTBOXVALUE)
select result;
You can search for the mechanism, how to where
in LINQ
on a List
.
If at all possible, try indexing your data using Lucene .NET or a similar search technology, like Solr or ElasticSearch. These technologies are optimized for search and provide you with lots of options to improve the ranking of your results. It can easily answer the question in your opening post and it's very fast. It would be very hard to implement this functionality on your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With