How to use Sql LIKE Operator % in LINQ

No.of Views1073
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  30 Dec 2010 22:12:33
Tag : LINQ , T-SQL
In this snippet, we will focus how to use the SQL LIKE operator in LINQ Query. The LINQ is powerful concepts to query data in .NET Framework level as like database query. But way of build query is different from the SQL query.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this snippet, we will focus how to use the SQL LIKE operator in LINQ Query. The LINQ is powerful concepts to query data in .NET Framework level as like database query. But way of build query is different from the SQL query.

Implementation

I have already built the DataSource for do LINQ Query for this demonstration. The sample records as like below,  

Image Loading

How to select records for exactly match?

LINQ Query

var data = from source in provider
           where source.GetString(1) == "Wrox"
           select source;

Sql Query

Select * from Source
Where Name='Wrox';

Output 

Image Loading

How to select records start with a word or character?

LINQ Query

var data=from source in provider where       
source.GetString(1).StartsWith("Wr") 
select source

Sql Query

SELECT * from Source
Where columnName LIKE 'Wr%'

Output

Image Loading

How to select records end with word or character?

LINQ Query

var data= from source in provider where 
source.GetString(1).EndsWith("ss") 
select source

Sql Query

Select * from source
Where columnName LIKE '%ss'

Output 

Image Loading

How to select records with word or character in column?

LINQ Query

var data= from source in provider 
where source.GetString(1).Contains("s") 
select source

Sql Query

Select * from source
Where columnName LIKE '%s%'

Output 

Image Loading

Hopes help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    In this snippet I am going to discuss about how to get list of duplicate items for the collection that we do in sql.
    Published Date : 05/Mar/2011
    Usage of Except Operator in LINQ
    Published Date : 28/May/2010
    Linq contains lots useful operators and i have found more two operators that can be help full in our day to day programming life. Here are explanation.
    Published Date : 21/Jun/2010
    Microsoft .NET framework 4.0 is having many features that make developers life very easy. Its also provides some enhancement to Linq also. I just found a great operator called Zip which merge the sequence of two entities.
    Published Date : 21/Jun/2010
    The following example shows how query execution is deferred until the results is enumerated.
    Published Date : 02/Sep/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top