0

例如,我有一个文本列表,每个文本看起来像这样:

我们更喜欢可以回答的问题,而不仅仅是讨论。提供详细信息。分享你的研究。如果你的问题是关于这个网站的

我想通过文本列表搜索can be answered并将上面的文本作为具有can be answered. 我该怎么做?

我试过Contains()了,但它什么也没返回。我的代码如下所示:

IEnumerable<App_ProjectTask> temp;
if (!String.IsNullOrEmpty(query))
{
    temp = dc.App_ProjectTasks.Where(x => x.Title.Contains(query) || x.Description.Contains(query));
    if (temp.Count() > 0)
    {
        results = temp.ToList();
    }
}
4

5 回答 5

4
String text = "We prefer questions that can be answered, "+
               "not just discussed.Provide details. Share your research."+
               "If your question is about this website";
if (text.Contains("can be answered"))
{
    Console.WriteLine("Text found");
}

上面的代码输出Text found.

要获取所有String包含文本的 s,请执行以下操作:

var query =
    from text in yourListOfTexts
    where text.Contains("can be answered")
    select text;
于 2012-06-26T16:30:05.223 回答
3

包含应该工作。

var strList = new List<String>();
string itemSearch = "string to search for";

foreach (string str in strList)
{
    if(str.Contains(itemSearch))
    {
        return str;
    }
}
于 2012-06-26T16:27:19.390 回答
0

这对我有用:这基本上是你所做的,但我展示了我的班级..

    private void WriteLine(String text)
    {
        textBox1.Text += text + Environment.NewLine;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<TestClass> list = new List<TestClass>();

        list.Add(new TestClass { Title = "101 unanswered questions", Description = "There are many questions which go unanswered, here are our top 1001" });
        list.Add(new TestClass { Title = "Best of lifes questions", Description = "Many of lifes questions answered" });
        list.Add(new TestClass { Title = "Top 10 smart answers", Description = "Top 10 smart answers for common interview questions" });

        var results =
            list.Where(x => x.Description.Contains("answered questions") | x.Title.Contains("answered questions"));
        foreach (TestClass res in results)
        {
            WriteLine(String.Format("Title: {0}, Desc: {1}", res.Title, res.Description));
        }
    }
}

public class TestClass
{
    public String Title;
    public String Description;
    public String Contents;

    public TestClass()
    {
        Title = "";
        Description = "";
        Contents = "";
    }
}
于 2012-06-26T17:22:34.013 回答
0

有一些提到使用 ToLower() 作为 contains 方法为了提高性能,您应该使用 string.IndoexOf("string", StringComparison.OrdinalIgnoreCase)

也就是说,如果 OP 需要他的搜索不区分大小写

不区分大小写的“包含(字符串)”

于 2012-06-26T20:11:10.223 回答
-1

尝试:

 IEnumerable<App_ProjectTask> temp;
if (!String.IsNullOrEmpty(query))
{
temp = dc.App_ProjectTasks.Where(x => x.Title.ToLower().Contains(query.ToLower()) || x.Description.ToLower().Contains(query.ToLower()));
if (temp.Count() > 0)
{
    results = temp.ToList();
}
}
于 2012-06-26T16:35:54.147 回答