This is a migrated thread and some comments may be shown as answers.

Selecting a Record Where a Text Column Contains a Key Word

2 Answers 137 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Aaron Drenberg
Top achievements
Rank 1
Aaron Drenberg asked on 16 Jun 2010, 12:23 AM

Hi,

Suppose I have this example table.

[SampleTable]
Id : PK, uniqueidentifier
Text: varchar(3000)

I've got a search method that accepts a list of key words as a parameter. What I'd like to do is select any records from SampleTable where the Text column contains any of the key words. For example:

public IQueryable<SampleTable> Search(List<string> keywords)
{
        return = from s in ObjectScope.Extent<SampleTable>()
                      where keywords.Any(k => s.Text.Any(t => t.Contains(k)))
                      select s;
}

I'd like to see if I can accomplish this using your LINQ support. Any suggestions on how to get it done? Thanks!

2 Answers, 1 is accepted

Sort by
0
Aaron Drenberg
Top achievements
Rank 1
answered on 16 Jun 2010, 01:05 AM

Looks like I've got it figured out.

In my query, I was trying to be efficient by consolidating the search into one statement. Instead, I tried using a foreach loop. I turned on event tracing, and the resulting SQL statement is executed with one round trip, so everything is working fine.

public IQueryable<SampleTable> Search(List<string> keywords)
{
        var query = from s in ObjectScope.Extent<SampleTable>() select s;
        foreach (string keyword in keywords)
        {
                query = from s in query
                             where s.Text.Any(t => t.Contains(keyword))
                             select s;
        }
        return query;
}

0
Accepted
Petko_I
Telerik team
answered on 17 Jun 2010, 05:56 PM
Hi Aaron Drenberg,

We are glad to see that you have resolved the issue. If you feel you need to know something else, you are welcome to ask.

Regards,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
LINQ (LINQ specific questions)
Asked by
Aaron Drenberg
Top achievements
Rank 1
Answers by
Aaron Drenberg
Top achievements
Rank 1
Petko_I
Telerik team
Share this question
or