10

I have an entity "POST" on my context and the following:

String[] keywords = new String[] { "Car", "Yellow" };

How can I search all POSTS which title contains the 2 words?

NOTE: keywords can have 1 to 4 words.

The post entity is the following:

public class Post {
  public Int32 Id { get; set; }
  public DateTime Created { get; set; }
  public String Text { get; set; }
  public String Title { get; set; }
  public DateTime Updated { get; set; }
} // Post

And here is my SQL:

create table dbo.Posts
(
  Id int identity not null 
    constraint PK_Posts_Id primary key clustered (Id),
  Created datetime not null,
  [Text] nvarchar (max) not null,
  Title nvarchar (120) not null,
  Updated datetime not null
);

I have been looking at LIKE in SQL but what is the equivalent in Entity Framework?

Do I need Full Text Search? And is it available in SQL Server 2012 Express?

UPDATE

Following haim770 suggestion I tried the following:

Context context = new Context();
String[] words = new String[] { "Car" };
List<Post> posts = context.Posts.Where(x => words.Contains(x.Title).ToList();

No posts were returned with this ... Any idea?

Thank You, Miguel

1
  • Please post an example of your context and the post entity. Commented Dec 12, 2013 at 15:06

2 Answers 2

11

you may try this

var keywords = new String[] { "Car", "Yellow" }.ToList();

var p = db.Posts.Where(q => keywords.Any(k => q.Title.Contains(k)));

And, if you are looking for titles containing All words in the keyword list, then as you said:

var p = db.Posts.Where(q => keywords.All(k => q.Title.Contains(k)))
Sign up to request clarification or add additional context in comments.

1 Comment

I think that is it ... And in your example Any is OR. If I would use ALL then I would get AND. What do you think?
7

Something like:

var keywords = new[] { "Car", "Yellow" };
var results = context.Posts.Where(x => keywords.Contains(x.Title));

The above will issue an SQL LIKE query.

If you want full-text search capabilities, first, you'll have to explicitly enable it in the database (you may have to install it if you're using the Express version), then use some solutions to integrate it with Entity Framework (probably using Entity Framdwork 6 'Interceptors').

6 Comments

One problem I am having is if the keyword is "Leao" and in the title is "leão" ... So I would need to ignore case and to ignore special characters. So "a" or "ã" is the same. Can I do this?
It depends on the column Collation. SQL_Latin1_General_CP1_CI_AI collation on your Title column will return both 'Leao' and 'leão'.
Can I set the collation to a column? Or should I set to the entire database? If yes, how can I do this? Do you know? I just added my SQL code. I suppose is better to set this on my T-SQL then letting SQL server to decide, right?
collation is another question. the query suggestion looks good.
You can set Collation to a specific column: [Text] nvarchar (max) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.