23

What is the correct syntax for this query?

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE '%@0%'", 'something');

Or should I use CHARINDEX?

Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206

6 Answers6

32

May be

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%something%");

xdazz
  • 158,678
  • 38
  • 247
  • 274
5

I haven't tried this, but I think it is worth trying:

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%" + "something" + "%");
Kevin Pullin
  • 13,122
  • 3
  • 24
  • 33
5

If you have done your mappings (wich the T4 will do for you) then you could infact do it like so:

var l=db.Fetch<article>("WHERE title LIKE @0", "%something%");

Saves some typing :)

David McLean
  • 1,462
  • 1
  • 12
  • 27
1

Can try like this also

var l=db.Fetch<article>("WHERE title LIKE @0", "%" + "something" + "%");
Hao
  • 103
  • 1
  • 2
  • 8
0

No need to write Select * From article, when you want to get all the fields. You are better off to use string.format to include the wildcards

var l=db.Fetch<article>("WHERE title LIKE @0", string.Format("%{0}%", yourVariable));
Yannick
  • 194
  • 8
-1
Articulo articulo = new Articulo();

articulo = db.SingleOrDefault<Articulo>("SELECT TOP (1) * FROM [Articulos] WHERE [CodigoEmpresa] = @0 and [CodigoArticulo] LIKE @1 ", CodigoEmpresa, codigoArticulo + "%");
A J
  • 3,970
  • 14
  • 38
  • 53