I need to build a query using linq to return the rows matching all words in an array:
Example array (splitKeywords):
{string[2]}
[0]: "RENAULT"
[1]: "CLIO"
KeywordSearch table:
public class KeywordSearch
{
// Primary properties
public int Id { get; set; }
public string Name { get; set; }
public Keyword Keyword { get; set; }
}
This table has the records:
Id: 1
Name: "RENAULT"
Keyword_Id: 3503
Id: 2
Name: "CLIO"
Keyword_Id: 3503
I want to get all the Keyword_Ids which match all the words in the array.
So far I have:
EDIT:
var keywordSearchQuery = _keywordSearchRepository.Query;
var keywordIds = from k in keywordSearchQuery
where splitKeywords.All(word => word.Equals((k.Name)))
select k.Keyword.Id;
But It's not working. Any ideas?
Thanks
keywordSearchQueryandsplitKeywords; and what does not working mean?from k in keywordSearchQuery where splitKeywords.Contains(k.Name)Your current query using .All is never going to work, as you are specifying a condition that has to be true for all the strings in the keywordSearchQuery array