<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