Selecting a Record Where a Text Column Contains a Key Word

3 posts, 1 answers
  1. Aaron Drenberg
    Aaron Drenberg avatar
    7 posts
    Member since:
    Jan 2010

    Posted 15 Jun 2010 Link to this post


    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. Aaron Drenberg
    Aaron Drenberg avatar
    7 posts
    Member since:
    Jan 2010

    Posted 15 Jun 2010 Link to this post


    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;
    }

  3. DevCraft banner
  4. Answer
    Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 17 Jun 2010 Link to this post

    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
Back to Top