0

For example, I have list of texts which each text looks like something like this:

We prefer questions that can be answered, not just discussed. Provide details. Share your research.If your question is about this website

and I want to search for can be answered through the list of text and return the text above as a text which has can be answered. How should I do this?

I tried Contains() but it returns nothing. My code looks like this:

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();
    }
}
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
ePezhman
  • 4,010
  • 7
  • 44
  • 80
  • Show your code for your current test please.. Contains will answer yes/no if exact match is found. – BugFinder Jun 26 '12 at 16:22
  • 2
    I think you want the SubString function - http://www.dotnetperls.com/substring – JMK Jun 26 '12 at 16:22
  • 3
    switch to linux and use grep ;) Sorry, but you know we were all thinking it :) – Devin Jun 26 '12 at 16:23
  • @BugFinder the code works fine if the query is only one word – ePezhman Jun 26 '12 at 16:29
  • Have you tried the code suggestions below - I added one, in my code, I've done roughly what you did however it works for me with multiple words, however if you mean you want to search for 2 words but in any order.. then let me know, I can change my answer to cover that its easy – BugFinder Jun 26 '12 at 19:55

5 Answers5

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");
}

The above code outputs Text found.

To get all the Strings that have the text do something like this:

var query =
    from text in yourListOfTexts
    where text.Contains("can be answered")
    select text;
NominSim
  • 8,447
  • 3
  • 28
  • 38
3

Contains should work.

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

foreach (string str in strList)
{
    if(str.Contains(itemSearch))
    {
        return str;
    }
}
infojolt
  • 5,244
  • 3
  • 40
  • 82
0

This works for me: Which is basically what you did, but I showed my class..

    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 = "";
    }
}
BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

There's a few mentions of using ToLower() for the contains method For performance you should use string.IndoexOf("string", StringComparison.OrdinalIgnoreCase)

That is if the OP needs his search to be case insensitive

Case insensitive 'Contains(string)'

Community
  • 1
  • 1
theedam
  • 629
  • 3
  • 10
-1

Try:

 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();
}
}
Ketchup
  • 3,071
  • 4
  • 20
  • 23