• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

How to use like operator in LINQ

i need to perform search operation using linq. suppose if the user enter a character 'p',i need to show all the records starts with that letter(both lower as well as captial letter records start's with p).
0
kravindra
Asked:
kravindra
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Craig WagnerSoftware ArchitectCommented:
from x in whatever
where x.SomeTextField.ToLower().StartsWith("p")
select x
0
 
Richard LeeSoftware EnthusiastCommented:
You can use something like the following:

DaTribe
var result = (from record in table
             where record.Name.ToLower() == 'p'
             select record).ToList(); 
or 
var result = (from record in table
             where record.Name.ToLower().StartsWith("p")
             select record).ToList(); 
or 
var result = table
   .Where(x => x.Name.ToLower()[0] = 'p')
   .Select(x => x)
   .ToList(); 
or 
var result = table
   .Where(x => x.Name.ToLower().StartsWith("p")
   .Select(x => x)
   .ToList();

Open in new window

0
 
AshokCommented:
string sDescript = "p"
sDescript = sDescript.ToUpper();

(from a in GetStoredProcedureResult
                                             where a.descript.ToUpper().StartsWith(sDescript)
                                             select a).FirstOrDefault();

BTW, if all characters in the database are already UPPERCASE, you do not need .ToUpper() in LINQ.

HTH
Ashok
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
AshokCommented:
Regarding that post

here is the link where I found the info (Original date: 08-11-2006)

http://codebetter.com/blogs/peter.van.ooijen/archive/2006/08/11/Change-a-database-connection-string-from-code.aspx

Not sure if it is still valid (I mean today).
0
 
AshokCommented:
Wrong place
0
 
x77Commented:
You can use any function from C# or Vb in Linq, also Like.
It generates a lambda.
Private t As DataTable

        Dim Query = From r In t Where DirectCast(r("MyColumna"), String) Like "%AnyThing%" Select r

Open in new window

0
 
Craig WagnerSoftware ArchitectCommented:
I don't understand... the author requested the question be deleted because he accepts an answer?
0
 
Richard LeeSoftware EnthusiastCommented:
Yes kravindra you have asked a question that has been comprehensively answered. Why request a delete?

DaTribe
0
 
Richard LeeSoftware EnthusiastCommented:
Accept posts:

ID: http:#a26064241
ID: http:#a26064307
ID: http:#a26099440

Split points
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now