Convert SQL Query into LINQ Expression - LIKE oper

2019-06-11 10:09发布

问题:

Can someone convert the following simple SQL statement into LINQ? The StudentID is of type int.

select * from Student where studentId like '%1001%';

Thank you!

回答1:

 ctx.Student.Where(x => x.StudentId.Contains("1001"))

It's strange that you have an ID of string type. Use int instead.

O, sorry, I see now, you wrote that ids are ints. In that case you cannot use like in SQL. It doesnt make sense. You must convert int to string first.

ctx.Student.Where(x => x.StudentId.ToString().Contains("1001"))


回答2:

Try this:

db.Students.Where(s => s.StudentID.ToString().Contains("1001"))


回答3:

Use the Contains operator:

from s in Student
where s.studentId.ToString().Contains("1001")
select s;