Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My parameters.SearchText is a List<string> containing search words. It seems this code returns a datarow that contains any string in the list. I want it to return only the datarows that contain ALL of strings in the list. Thoughts?


C#
var query1 =
         from nodes in Datasets.Datasets.dsQuestions.Tables["key"]
         from _searchtext in parameters.SearchText
             where 
             nodes.Field<string>("Question").ToUpper().Contains(_searchtext.ToUpper())                         
         select nodes;



Update: I took out the line to search Keywords since it was irrelevant.
Posted
Updated 10-Jul-12 5:36am
v5

Use && instead of ||..
So it will become
C#
var query1 =
    from nodes in Datasets.Datasets.dsQuestions.Tables["key"]
    from _searchtext in parameters.SearchText
    where
        nodes.Field<<string>("Question").ToUpper().Contains(_searchtext.ToUpper())
        &&
        nodes.Field<string>("Keyword").ToUpper().Contains(_searchtext.ToUpper())
    select nodes;
 
Share this answer
 
v3
Comments
Member 9230112 10-Jul-12 11:13am    
That doesn't fix the problem. What is needed is to return all datarows that contain all the strings in SearchText found in either the Question or Keyword field. This code will return a datarow that contains at least one string in the SearchText.
C#
var query1 = Datasets.Datasets.dsQuestions.Tables["key"].Select(k=> parameters.SearchText.All(x => k.Field<string>("Question").ToUpper().Contains(x.ToUpper())
		 || k.Field<string>("Keyword").ToUpper().Contains(x.ToUpper())));
 
Share this answer
 
v2
Comments
Member 9230112 10-Jul-12 11:23am    
This gives me the following error:

Cannot convert lambda expression to type 'string' because it is not a delegate type
var query1 = Datasets.Datasets.dsQuestions.Tables["key"].Rows.OfType<datarow>().Where(k => parameters.SearchText.All(x => k.Field<string>("Question").ToUpper().Contains(x.ToUpper())));


Thanks to Damith for pointing me in the right direction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900