Avatar of adskarcox
Flag 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
.NET ProgrammingASP.NETSQL

Avatar of undefined
Last Comment

8/22/2022 - Mon

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


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


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


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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question