如何在c#类中搜索字符串

时间:2022-09-13 07:56:28

I am developing an app in which i have some data fetched from net into a class. Class is

我正在开发一个应用程序,在这个应用程序中,我从net获取一些数据到一个类中。类是

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 ?"

现在我想搜索一个字符串q="哪个经理毕业于美国?"

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 ?

我的方法对搜索足够好吗?还是有更好的选择?

4 个解决方案

#1


2  

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.

您正在开发的是一种人类可读可写的查询语言。从一个字符串开始,可能是一个,嗯,开始,但是想象一下搜索的可能性:在一个城市或城市的范围内搜索人们,搜索那些在500强公司或某一领域工作的人。

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.

为此,您应该开发一种查询语言。具有易于修改和文档化的语法。看看ANTLR解析器生成器,它与c# /. net配合得很好。

#2


2  

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.

我对你想要实现的机制有点担心,如果用户类型q=“经理从美国毕业?”,意思是不要把“which”放在里面,所以你需要像ANTLR那样去查询语言。

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:

将这些值传递给方法后,使用LINQ从集合中获取数据,如:

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.

您可以搜索机制,如何在列表的LINQ中找到。

#3


1  

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.

如果可能的话,尝试使用Lucene . net或类似的搜索技术(如Solr或ElasticSearch)为数据建立索引。这些技术针对搜索进行了优化,并为您提供了许多选项来提高结果的排名。它可以很容易地回答你的问题,而且非常快。你很难自己实现这个功能。

#4


0  

If you have a number of fields which you KNOW will exist - like Education and Work - I would recommend having an array of synonyms and words-related-to for each field.

如果您有许多您知道将存在的字段——比如教育和工作——我建议您为每个字段设置一个同义词和单词相关的数组。

So, for Education, you could have a list of words like this:

所以,对于教育,你可以有一个这样的单词列表:

Graduate, University, School, Learning, Taught e.t.c

Do something like this?

是这样的吗?

Obviously, you won't be able to match EVERY single word, but it might be worth a shot?

显然,你不可能把每一个字都拼出来,但这可能值得一试。

Or you could try including tags in each field, rather than just words. So rather than them putting "I graduated from University", you have an Education field, and then a University sub-field, where they put the name.

或者您可以尝试在每个字段中包含标记,而不仅仅是单词。与其写“我大学毕业了”,你有一个教育领域,然后是一个大学子领域,在那里他们写上名字。

It's all about limiting errors, bearing in mind humans can make millions of errors that you just CAN'T deal with programmatically.

这一切都是为了限制错误,记住人类可以犯数百万个错误,而这些错误是你无法以编程方式处理的。

#1


2  

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.

您正在开发的是一种人类可读可写的查询语言。从一个字符串开始,可能是一个,嗯,开始,但是想象一下搜索的可能性:在一个城市或城市的范围内搜索人们,搜索那些在500强公司或某一领域工作的人。

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.

为此,您应该开发一种查询语言。具有易于修改和文档化的语法。看看ANTLR解析器生成器,它与c# /. net配合得很好。

#2


2  

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.

我对你想要实现的机制有点担心,如果用户类型q=“经理从美国毕业?”,意思是不要把“which”放在里面,所以你需要像ANTLR那样去查询语言。

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:

将这些值传递给方法后,使用LINQ从集合中获取数据,如:

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.

您可以搜索机制,如何在列表的LINQ中找到。

#3


1  

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.

如果可能的话,尝试使用Lucene . net或类似的搜索技术(如Solr或ElasticSearch)为数据建立索引。这些技术针对搜索进行了优化,并为您提供了许多选项来提高结果的排名。它可以很容易地回答你的问题,而且非常快。你很难自己实现这个功能。

#4


0  

If you have a number of fields which you KNOW will exist - like Education and Work - I would recommend having an array of synonyms and words-related-to for each field.

如果您有许多您知道将存在的字段——比如教育和工作——我建议您为每个字段设置一个同义词和单词相关的数组。

So, for Education, you could have a list of words like this:

所以,对于教育,你可以有一个这样的单词列表:

Graduate, University, School, Learning, Taught e.t.c

Do something like this?

是这样的吗?

Obviously, you won't be able to match EVERY single word, but it might be worth a shot?

显然,你不可能把每一个字都拼出来,但这可能值得一试。

Or you could try including tags in each field, rather than just words. So rather than them putting "I graduated from University", you have an Education field, and then a University sub-field, where they put the name.

或者您可以尝试在每个字段中包含标记,而不仅仅是单词。与其写“我大学毕业了”,你有一个教育领域,然后是一个大学子领域,在那里他们写上名字。

It's all about limiting errors, bearing in mind humans can make millions of errors that you just CAN'T deal with programmatically.

这一切都是为了限制错误,记住人类可以犯数百万个错误,而这些错误是你无法以编程方式处理的。