Link to home
Start Free TrialLog in
Avatar of adskarcox
adskarcoxFlag for United States of America

asked on

Users Searches with Linq

I've got a Biography column in a Users table that holds the user's biography.  This column could contain 1 sentence or 5 paragraphs of text.

I need a keyword search to search this biography column for the search word.  I would like for the search to search each separate word in the biography and only search the first of the word.  Example:

Biography: "My name is Bob and I was born in Kentucky."

Searching for "kent" WOULD match this biography because "kent" is contained in the first part of the word "Kentucky".  Because the search will only search the FIRST part of each word, searching for "tucky" would not match any words in this biography.

What would the be the best way to design a linq query to accomplish this?

Thank you in advance.

Environment: C#.NET 4.0
Avatar of BuggyCoder
BuggyCoder
Flag of India image

assuming that entity for this table is known as users, you need to do this linq to entity query:-

users.where(u=>u.Biography.Contains("Kent"));

this will work in EF 4.0, for previous versions, here is how to use like operator:-

http://startbigthinksmall.wordpress.com/2011/07/30/found-a-draft-linq-to-entities-is-great-for-demos/
http://stackoverflow.com/questions/3095781/how-to-use-sql-like-with-linq-to-entities
Hi,
Following Query will find the Name of the city where it contains "Mum"
It will find the records if its starting with "Mum" or any internal word is starting with "Mum".
out of the following values it will find only 2 result as per your requirement

new Customer{CustID="a",City="Navi Mumbai",CustName="Me"},
new Customer{CustID="g",City="PuneMum",CustName="Me"},
new Customer{CustID="h",City="Mum Pune",CustName="Me"},
It will find 1st and 3rd Records only.

Check the space before Mum in 1st condition in below query

           
 var results = from c in LoadCustomers()
                          [b]where c.City.Contains(" Mum") || c.City.StartsWith("Mum")[/b]
                          group c by c.City into g
                          select new {CustName = c.CustName ,City = c.City};

Open in new window

Avatar of adskarcox

ASKER

BuggyCoder and darjimaulik - thank you for your replies.  I do not want to use .contains() however, because using .contains() returns a lot of junk results.

What I need is a way to search each word in a sentence using .startswith() or maybe there is a better way.
ASKER CERTIFIED SOLUTION
Avatar of darjimaulik
darjimaulik
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial