Users Searches with Linq

adskarcox
adskarcox used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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

Author

Commented:
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.
Hi,
.Startswith will find the result where the string starts with the search term.
If you want to find a search term which is beginning of any internal word then you have to use .contains.
I have specifically suggested a whitespace before the search term so it will find the beginning of the word.

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.

my query will find the "kent" in the string and it will return zero result for "tucky".

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial