LINQ to Entities does not recognize the 'Boolean Like' method?

Asked

Viewed 386 times

0

I am having this error when trying to make one with query using LINQ to SQL after making the suggested changes:

{SELECT
`Extent1`.`idcliente`, 
`Extent1`.`nome`, 
`Extent1`.`pai`, 
`Extent1`.`mae`, 
`Extent1`.`informacao`, 
`Extent1`.`datanascimento`, 
`Extent1`.`foto`
FROM `cliente` AS `Extent1`
 WHERE (LOCATE(TRIM(@p__linq__0), `Extent1`.`mae`)) = 1}

C#

    [HttpPost]
    public ActionResult Index(string recebeNome, int recebeOpcao)
    {


        try
        {
            sistema_mobileEntities dao = new sistema_mobileEntities();
            IQueryable<cliente> sql;
            sql = null;

            if (recebeOpcao == 1)
            {

                //link to sql não tem like
                sql = from c in dao.cliente
                      where c.nome.StartsWith(recebeNome.Trim())
                      // where SqlMethods.Like(c.nome, recebeNome.Trim() + "%")
                      select c;

                TempData["opcao1"] = "nome";
            }

            if (recebeOpcao == 2)
            {
                sql = from c in dao.cliente
                      where c.pai.StartsWith(recebeNome.Trim())
                      select c;

                TempData["opcao2"] = "pai";
            }

            if (recebeOpcao == 3)
            {
                sql = from c in dao.cliente
                      where c.mae.StartsWith(recebeNome.Trim())
                      select c;

                TempData["opcao3"] = "mae";
            }

            return View(sql.ToList());
        }

        catch (Exception ex)
        {
            return Json("Erro ao consultar cliente" + ex.Message);
        }


    }
  • Did any help you more? You need something to be improved?

2 answers

2

If you were using LINQ To SQL you would need to ensure that you are using namespace with using System.Data.Linq.SqlClient. But in LINQ To Entities there is no such method. You would have to create a method that simulates a LIKE or use another form.

In this specific case could use only

where c.nome.StartsWith(recebeNome.Trim())

I put in the Github for future reference.

  • I made the code change, and I have the sql query generated! I just need to know if it is correct this way.

0

Use SqlFunctions.PatIndex instead of SqlMethods.Like, if this is not available.

  • The class SqlFunctions is defined in Assembly System.Data.Entity.dll

  • whereas the class SqlMethods is defined in Assembly System.Data.Linq.dll

  • Using Where c.nome.Startswith(receivedName.Trim()) I have the sql answer, I just need to know if it is correct: {SELECT Extent1.idclient, Extent1.name, Extent1.father, Extent1.mae, Extent1.information, Extent1.databirth, Extent1.photo FROM client AS Extent1 WHERE (LOCATE(TRIM(@p__linq_0), Extent1.mae)) = 1}

  • What is the database system you are using?

  • I am using Mysql Database

Browser other questions tagged

You are not signed in. Login or sign up in order to post.