Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search a String in Class in c#

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 ?

like image 877
Man8Blue Avatar asked Jul 10 '12 07:07

Man8Blue


People also ask

How do I find a word in a string in C?

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.

What is search string in C?

(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.

Can you scan a string in C?

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.).

How do you search for an element in a string?

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.


3 Answers

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.

like image 123
saintedlama Avatar answered Oct 18 '22 07:10

saintedlama


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.

like image 22
Imran Balouch Avatar answered Oct 18 '22 08:10

Imran Balouch


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.

like image 32
Arjen Avatar answered Oct 18 '22 08:10

Arjen