473,320 Members | 1,814 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,320 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 17715
<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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.