• 15
  • Apr

how-to search mutiple keywords using LINQ to SQL

this bit of LINQ code will let you search for records using any number of keywords

it’s a bit limited, but it illustrates the starting point for something more.

the key is to get results for the first keyword into a disconnected object (in this case, a simple List<String>) and then search for the remaining keywords in there. clearly you could up the ante, and select more fields and spin up a collection of custom objects (i.e. List<MyClass>) but this suited my needs for an autocomplete for country names.

// GetMatchesIntersect(string csv, int maxMatches)
// mainly developed for auto-complete scenarios
//
// searches single [n]varchar|text|char field for multiple keywords
//
// returns array of field values containing ALL keywords
//
// NOTE: i decided on array because i am implementing this a webservice
//       to be consumed by other languages/platforms)
public string[] GetMatchesIntersect(string csv, int maxMatches)
{

    // keywords come in as comma-separated values
    string[] keywords = csv.Split(new char[] { ' ', ',' });

    // this list will hold the matches
    List<string> matches;

    using (DbDataContext db = new DbDataContext())
    {

     // build list of matches for first keyword into generic List<string>
     matches = (from s in db.TableToSearch
                where s.FieldToSearch.Contains(keywords[0])
                select s.FieldToSearch).ToList<string>();

     // if any were found
     if (null != matches && matches.Count > 0)
     {
         // start searching at the 2nd keyword (if any)
         for (int i = 1; i < keywords.Length && matches.Count > 0; i++)
         {
          // using .ToUpper() because matching in a List<string> is case-sensitive
          // set matches equal to the query results for each succesive keyword.
          matches = (from match in matches
                  where match.ToUpper().Contains(keywords[i].ToUpper())
                  select match).ToList<string>();
         }
     }
    }
    // ship it out as array
    return matches.Take(maxMatches).ToArray<string>();
}

» You can leave a comment, or trackback from your own site.

55 Comments

  1. natural treatment for hemorrhoids Says:

    natural treatment for hemorrhoids…

    groggyjava.tv » Blog Archive » LINQ to SQL multi-keyword search…

  2. James Says:

    < a href = “http://list.albumpass.ru/?p=33&lol= othons@fumbling.unblushing“>.< / a >…

    ????? ?? ????….

  3. Evan Says:

    < a href = “http://cat.albumorbit.ru/?p=1&lol= inc@ballots.means“>.< / a >…

    good info!!…

  4. vincent Says:

    < a href = “http://eu.artistcat.ru/?p=48&lol= unlike@backside.khaki“>.< / a >…

    tnx!!…

  5. Jaime Says:

    < a href = “http://en.songway.ru/?p=16&lol= ailments@kraft.portrays“>.< / a >…

    áëàãîäàðåí!…

  6. Edgar Says:

    < a href = “http://contagion.artistnote.ru/?p=35&lol= nagle@reunion.taller“>.< / a >…

    ñïñ….

  7. cameron Says:

    < a href = “http://en.artistbus.ru/?p=24&lol= whispering@argon.numbness“>.< / a >…

    hello!…

  8. Lonnie Says:

    < a href = “http://epicycle.songloft.ru/?p=22&lol= artful@crucially.obstinate“>.< / a >…

    ñïñ!!…

  9. glenn Says:

    < a href = “http://com.albumyard.ru/?p=37&lol= prompted@drifted.vegetarian“>.< / a >…

    áëàãîäàðåí!!…

  10. Jay Says:

    < a href = “http://org.albumdock.ru/?p=50&lol= solstice@statuses.graces“>.< / a >…

    hello!!…

  11. wade Says:

    < a href = “http://fr.songtect.ru/?p=35&lol= prefixes@mouthpieces.whirring“>.< / a >…

    ñýíêñ çà èíôó!…

  12. herman Says:

    < a href = “http://gov.songkeeper.ru/?p=7&lol= obeying@intellectuals.contributions“>.< / a >…

    ñïñ çà èíôó….

  13. Floyd Says:

    < a href = “http://uk.artistcutter.ru/?p=11&lol= bind@electricity.terrifies“>.< / a >…

    ñïñ çà èíôó….

  14. lee Says:

    < a href = “http://net.artistbus.ru/?p=24&lol= spooky@galahad.stifling“>.< / a >…

    ñïñ!!…

  15. troy Says:

    < a href = “http://catalog.rnblyrics.ru/?p=22&lol= vassal@lose.austria“>.< / a >…

    thank you….

  16. Derrick Says:

    < a href = “http://eu.albumtory.ru/?p=46&lol= whynt@torpor.auto“>.< / a >…

    thank you….

  17. ricardo Says:

    < a href = “http://obsequious.artistcrew.ru/?p=28&lol= pens@armful.vilas“>.< / a >…

    thanks….

  18. Scott Says:

    < a href = “http://cn.artiststation.ru/?p=24&lol= encourages@viscera.patter“>.< / a >…

    áëàãîäàðñòâóþ….

  19. ronald Says:

    < a href = “http://uk.mp3path.ru/?p=8&lol= varlaam@bays.obliquely“>.< / a >…

    áëàãîäàðåí….

  20. eduardo Says:

    < a href = “http://eu.songshire.ru/?p=40&lol= pad@idolize.falloff“>.< / a >…

    tnx for info!!…

  21. derrick Says:

    < a href = “http://shop.artistcycle.ru/?p=35&lol= strengtened@waggled.unos“>.< / a >…

    áëàãîäàðñòâóþ….

  22. francis Says:

    < a href = “http://gregorys.artistfish.ru/?p=14&lol= wildenstein@trusteeship.cooking“>.< / a >…

    áëàãîäàðþ!…

  23. Dustin Says:

    < a href = “http://wp.songloft.ru/?p=28&lol= snelling@flawless.lbbod“>.< / a >…

    ñýíêñ çà èíôó….

  24. Brian Says:

    < a href = “http://ru.songbit.ru/?p=8&lol= adorns@antipodes.judgments“>.< / a >…

    ñýíêñ çà èíôó….

  25. Pedro Says:

    < a href = “http://prominent.49p.ru/?p=3&lol= chambermaid@pretense.ladylike“>.< / a >…

    ñïñ….

  26. derrick Says:

    < a href = “http://shop.albumwork.ru/?p=24&lol= wheelock@cartridges.circulate“>.< / a >…

    ñïñ!!…

  27. Fredrick Says:

    < a href = “http://org.artistcycle.ru/?p=24&lol= briefing@muscle.disbelieves“>.< / a >…

    tnx for info!…

  28. Johnnie Says:

    < a href = “http://redheader.artistovator.ru/?p=16&lol= unreleased@injured.roys“>.< / a >…

    áëàãîäàðþ!…

  29. Lee Says:

    < a href = “http://net.songtect.ru/?p=15&lol= sided@patmore.doubte“>.< / a >…

    ñýíêñ çà èíôó!…

  30. Philip Says:

    < a href = “http://overcurious.footpaths.ru/?p=15&lol= barn@teeeee.oceana“>.< / a >…

    ñïàñèáî çà èíôó!!…

  31. Bob Says:

    < a href = “http://gov.songport.ru/?p=15&lol= saint@horribly.ditches“>.< / a >…

    tnx for info!…

  32. Jacob Says:

    < a href = “http://wp.albumency.ru/?p=27&lol= fluxes@neurotic.ky“>.< / a >…

    ñïàñèáî çà èíôó!!…

  33. michael Says:

    < a href = “http://gov.artistscript.ru/?p=32&lol= bartender@respective.glacier“>.< / a >…

    tnx for info!…

  34. christopher Says:

    < a href = “http://salesman.46p.ru/?p=20&lol= thinned@residences.extinguish“>.< / a >…

    áëàãîäàðñòâóþ!…

  35. Ben Says:

    < a href = “http://com.songsquad.ru/?p=12&lol= pirate@occupants.drinking“>.< / a >…

    ñýíêñ çà èíôó!…

  36. Tracy Says:

    < a href = “http://wp.asphaltirovanie.ru/?p=25&lol= vigor@hetman.statuette“>.< / a >…

    ñïñ çà èíôó….

  37. ian Says:

    < a href = “http://catalog.albumtrail.ru/?p=24&lol= yonder@genre.unadorned“>.< / a >…

    ñïàñèáî çà èíôó….

  38. Mario Says:

    < a href = “http://suez.songcruiser.ru/?p=20&lol= intolerance@integrity.devious“>.< / a >…

    ñïàñèáî!!…

  39. norman Says:

    < a href = “http://slang.songcrop.ru/?p=17&lol= loses@approved.income“>.< / a >…

    tnx!!…

  40. Clayton Says:

    < a href = “http://greatness.songfox.ru/?p=42&lol= lawyers@hoagy.plucking“>.< / a >…

    thank you….

  41. harry Says:

    < a href = “http://list.songway.ru/?p=29&lol= spectators@essences.burned“>.< / a >…

    áëàãîäàðåí!!…

  42. Cory Says:

    < a href = “http://list.albumpass.ru/?p=8&lol= regulations@gardenia.doctored“>.< / a >…

    ñïñ çà èíôó….

  43. Peter Says:

    < a href = “http://dogleg.instrumentallyrics.ru/?p=39&lol= petted@wailed.obtained“>.< / a >…

    ñïñ çà èíôó!…

  44. arturo Says:

    < a href = “http://ch.songfox.ru/?p=7&lol= inhomogeneous@allstates.paddies“>.< / a >…

    ñïñ çà èíôó….

  45. Alex Says:

    < a href = “http://amaral.songtor.ru/?p=8&lol= bovine@substerilization.playoff“>.< / a >…

    ñïàñèáî!…

  46. benjamin Says:

    < a href = “http://wp.artistcluster.ru/?p=21&lol= rag@drain.sue“>.< / a >…

    ñïñ!!…

  47. samuel Says:

    < a href = “http://ch.songferry.ru/?p=50&lol= tillies@trumbull.ottauquechee“>.< / a >…

    ñïñ!!…

  48. Tracy Says:

    < a href = “http://shop.mp3optic.ru/?p=26&lol= infrequent@flattering.sneering“>.< / a >…

    áëàãîäàðñòâóþ!…

  49. calvin Says:

    < a href = “http://export.artistgroup.ru/?p=34&lol= blunderings@voroshilov.purchasers“>.< / a >…

    áëàãîäàðþ….

  50. Scott Says:

    < a href = “http://uk.songseller.ru/?p=39&lol= chion@vocalization.evident“>.< / a >…

    ñïàñèáî çà èíôó….

  51. brandon Says:

    < a href = “http://sponsors.songnik.ru/?p=4&lol= commissary@substantiate.established“>.< / a >…

    ñïñ….

  52. dwight Says:

    < a href = “http://gov.artistcrew.ru/?p=11&lol= evolution@scandals.abell“>.< / a >…

    ñïñ….

  53. Theodore Says:

    < a href = “http://com.mp3path.ru/?p=7&lol= erik@structure.borrow“>.< / a >…

    ñïñ çà èíôó!…

  54. Aaron Says:

    < a href = “http://net.reggaemp3.ru/?p=16&lol= stereophonic@corruptible.warped“>.< / a >…

    hello!!…

  55. brett Says:

    < a href = “http://shop.artistovator.ru/?p=25&lol= tried@indicating.caron“>.< / a >…

    thanks for information!!…

Leave a Comment

You must be logged in to post a comment.