473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

"LIKE" operator in LINQ to SQL?

I am just getting to grips with LINQ to SQL and my first attempt is to
create a Search form.

I need the LINQ to generate SQL like this:

SELECT * FROM CustomerTable WHERE Surname LIKE 'S%'

My LINQ so far looks like this:

var cons = from c in db.CustomerTable
where c.Surname LIKE 'S%'
select c;

However, it doesn't like "LIKE". How might I mimic this in LINQ? I
should add that I'm hardly the world's most experienced C# programmer
either!

Thanks

Edward
Sep 10 '08 #1
4 17735
<te********@hotmail.comwrote:
I am just getting to grips with LINQ to SQL and my first attempt is to
create a Search form.

I need the LINQ to generate SQL like this:

SELECT * FROM CustomerTable WHERE Surname LIKE 'S%'

My LINQ so far looks like this:

var cons = from c in db.CustomerTable
where c.Surname LIKE 'S%'
select c;

However, it doesn't like "LIKE". How might I mimic this in LINQ? I
should add that I'm hardly the world's most experienced C# programmer
either!
No, it wouldn't like "LIKE" - you're not actually writing SQL here,
you're writing C# which is translated into SQL.

The C#/.NET way of checking whether one string starts with another is
to use the StartsWith method. Try changing your query to:

var cons = from c in db.CustomerTable
where c.Surname.StartsWith("S")
select c;
It may be worth noting that if you're only using one or two query
operators, the normal C# syntax can often end up being simpler:

var cons = db.CustomerTable.Where(c =c.Surname.StartsWith("S"));

That's basically what the compiler was doing anyway.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #2
It may be worth noting that if you're only using one or two query
operators, the normal C# syntax can often end up being simpler:
That is *especially* true of search forms (from the OP), as you can
compose multiple (optional) restrictions more easily:

IQueryable<Fooquery = db.CustomerTable;
if(!string.IsNullOrEmpty(surname))
{
query = query.Where(c =c.Surname.StartWith(surname));
}
if(!string.IsNullOrEmpty(forename))
{
query = query.Where(c =c.Surname.StartWith(forename));
}
if(dob != null) // assumes DateTime? for dob
{
DateTime dobActual = dob.Value.Date;
query = query.Where(c =c.DateOfBirth == dobActual);
}
// etc

Marc
Sep 10 '08 #3
No more having to append strings ending with "and" and then having to trim
off the last 4 chars of the query string when you have finished building it
:-)

Sep 10 '08 #4
Peter Morris wrote:
No more having to append strings ending with "and" and then having to
trim off the last 4 chars of the query string when you have finished
building it :-)
Meh. My ORM has done that for me for many years :-)

The real advantage is:

No more having to maintain code where somebody keeps appending strings
ending with "and" and then having to trim off the last 4 chars of the
query string when they have finished building it.

I think that's still some years away though.

Alun Harford
Sep 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hugh Welford | last post by:
Hi I cant seem to get the LIKE oprator to work in asp. I am trying to verify e-mail input format by using request("email") like "*@*.*" but it returns "sub or function not defined" any...
2
by: Adam Short | last post by:
I've never needed it before but now I do! Does anyone know if you are able to perform a NOT Like search using Classic ASP ADO? i.e. myData.Filter = "Ref NOT LIKE '*1234*'" by the way this...
2
by: Big John | last post by:
in Access 2003 I am trying to get sql query to run using a field as input, but I want to input into the field a value that uses the "like" operator to find any value that starts with the value...
4
by: news.microsoft.com | last post by:
We need to take a large amount of data, load it into memory, and then search on it. A hashtable would work if you could perform operations similar to sql select .. where like ... clauses. Any...
3
by: Derrick | last post by:
Is there a C# "Like" statement for comparing two strings? if(stringA Like "%diabe%mel%) do stuff... Thanks in advance! Derrick
1
by: Craig Kenisston | last post by:
Hi, I need to write a function that should behave like the SQL's "like" operator on a list of words. I was wondering if I can use Regex directly to do this job. But I've been reading about...
1
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many...
11
by: Bruce Lawrence | last post by:
Ok, I'm baffled... I'm making a query in access 97 between 2 tables. There is a field in both tables called "DWGNO". OPENORD has a record with a DWGNO of "00000012345" DIEDATA has a record...
2
by: swingingming | last post by:
Hi, When I am playing around with options, I came across with enabling "SQL server compatible syntax". After I enable this, I could not get one of my query to run. Can anyone explain the subtle...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.