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();
}
}
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 String
s that have the text do something like this:
var query =
from text in yourListOfTexts
where text.Contains("can be answered")
select text;
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;
}
}
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 = "";
}
}
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
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.