0

How do you check if all the search terms exist in a specific field.

search terms option 1: green white,

search terms option 2: test query,

search terms option 3: green query.

var search_result = (from s in dbContext.databasetable.Where
    (i => 1.city == "NYC" 
    &&(search_text.Any(x => i.name.Contains(x)) 
    || search_text.Any(x => i.surname.Contains(x))))
    select s).OrderByDescending(i => i.Date);

search_text is an string array

record in database:

name: green white red

surname: search test query

option 1 and two must return the record while option 3 must return "null".

The word must all exist in the name or must all exist in the surname.

Babulaas
  • 761
  • 3
  • 13
  • 47
  • You have `from s in dbContext.databasetable` but your `.Any(x => i.name.Contains(x))` uses `i`, not `s`. Is this a typo in your question, or is it also in your actual code? – Sergey Kalinichenko Aug 26 '15 at 14:45
  • var search_result = (from s in dbContext.databasetable.Where (i => 1.city == "NYC" &&(search_text.Any(x => i.name.Contains(x)) || search_text.Any(x => i.surname.Contains(x)))) select s).OrderByDescending(i => i.Date); i have some other selections before the name and surname search is something wrong here? – Babulaas Aug 26 '15 at 14:46
  • I'm curious why you need the `from s in` part, because everything below it is in fluent syntax anyway? – Sergey Kalinichenko Aug 26 '15 at 14:50

2 Answers2

0

Did you think about the casing?

It's not your problem in this current example but I guessed that maybe you didn't show us your real data. It's case sensitive, so "GREEN" won't match "green".

Gabriel
  • 333
  • 2
  • 8
0

Stupid me.

This was just some test data for the question. in my case it was subject and content. I used the subjectdata also in the contentdata. in other words the query is working fine with .All

The testdata was incorrect. Thanks for your replies.

Babulaas
  • 761
  • 3
  • 13
  • 47