Reputation: 305
I have a working query that returns 140 rows from the db:
string tag = "New York";
string sql = "select " +
" T.Id TagId, " +
" PM.Id MachineId, " +
$" '{tag}' TagSiteName, " +
" T.Name TagCpeMachineLine, " +
" PM.Plant, " +
" PM.Line, " +
" PM.Type " +
"from Tags T " +
"inner join PaperMachines PM " +
" ON PM.Id = T.PaperMachineId " +
$"WHERE T.Name LIKE ( '{tag}' ) ";
var result = db.Database.SqlQuery<TagsMachines>(sql);
When I try to parameterize it, I get 'Enumeration yielded no results' from the result.
string tag = "New York";
string sql = "select " +
" T.Id TagId, " +
" PM.Id MachineId, " +
" '@tag' TagSiteName, " +
" T.Name TagCpeMachineLine, " +
" PM.Plant, " +
" PM.Line, " +
" PM.Type " +
"from Tags T " +
"inner join PaperMachines PM " +
" ON PM.Id = T.PaperMachineId " +
"WHERE T.Name LIKE ( '@tag' ) ";
var result = db.Database.SqlQuery<TagsMachines>(sql, new SqlParameter("@tag", tag));
I'm not sure what's going on here. Any ideas?
Upvotes: 0
Views: 158
Reputation: 76
Remove the @tag from top of your Query and at the bottom of your Query make it
T.Name = @tag or T.Name like ('%' + @tag +'%')
if you want like contains.
Finally at the end of the result line add a .FirstOrDefault().
Upvotes: 1