How to search with Linq query in multiples columns and bind into datagridview?

0

I am using C# and Entity Framework and I would like to select in database with some filter condition. Which comes from a simple SQL query like this:

SELECT * 
FROM EMPLOYEE 
WHERE ACTIVE = 1 (FNAME LIKE '%KEY%' OR LNAME LIKE '%KEY%' OR ADDRESS LIKE '%KEY%') 
ORDER BY LASTUPDAATE DESC;

I using in Linq query as below:

var query = (from e in db.TBLEMPLOYEE
             where (e.ACTIVE == 1 AND 
                   (e.FNAME.Contains(text.ToString().Trim())
                   || e.LNAME.Contains(text.ToString().Trim())
                   || e.ADDRESS.Contains(text.ToString().Trim())))
             select e).OrderByDescending(e => c.LASTUPDATE);

if (query.Any())
{
    int i = 0;

    foreach (EMPLOYEE item in query)
    {
        i += 1;

        int newrow = grid.Rows.Add();
        grid.Rows[newrow].Cells[0].Value = item.ID.ToString();
        grid.Rows[newrow].Cells[1].Value = i.ToString();
        grid.Rows[newrow].Cells[2].Value = item.FNAME.ToString();
        grid.Rows[newrow].Cells[3].Value = item.LNAME.ToString();
        grid.Rows[newrow].Cells[4].Value = item.ACTIVE.ToString();
    }
}

But I get an error in the linq query while running:

The function evaluation requires all threads to run.
Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.

Any suggestions please?

Thank you in advance.

Ada.

c#
sql-server
entity-framework
asked on Stack Overflow Mar 22, 2021 by Harina Ada • edited Mar 22, 2021 by marc_s

2 Answers

0

Fix your query:

var seachText=text.Trim();

 var query =db.TBLEMPLOYEE
            .Where( e=> (e.ACTIVE == 1)
 && ( ( EF.Functions.Like(e.FNAME,$"%{seachText}%")
|| ( EF.Functions.Like(e.LNAME,$"%{seachText}%")
 || ( EF.Functions.Like(e.ADDRESS,$"%{seachText}%")  ) )
         .OrderByDescending(c => c.LASTUPDATE)
       .ToList();
answered on Stack Overflow Mar 22, 2021 by Serge • edited Mar 22, 2021 by Serge
0

Here is the working.

 var query = (from e in db.TBLEMPLOYEE
         where (e.ACTIVE == 1 AND 
               (e.FNAME.Contains(text.ToString().Trim())
               || e.LNAME.Contains(text.ToString().Trim())
               || e.ADDRESS.Contains(text.ToString().Trim())))
         select e).OrderByDescending(e => c.LASTUPDATE).ToList();

Thank you very much all.

answered on Stack Overflow Mar 23, 2021 by Harina Ada

User contributions licensed under CC BY-SA 3.0