EF 实现Mysql IN功能的两种方式
通过linq以及 sql参数化查询方式可以实现
using (MysqlTestDB db = new MysqlTestDB()) { //linq List<int> idList = new List<int>() { 1, 2 }; var retLinq = db.tb_tests.Where(m => idList.Contains(m.id)); foreach (var item in retLinq) { Console.WriteLine(item.id + "," + item.name); } //sql 参数化查询 string sql = @"select * from tb_test where FIND_IN_SET (id,@ids)"; //Find_IN_SET 为mysql函数 var paramList = new List<MySqlParameter> { new MySqlParameter("@ids", string.Join(",",idList.ToArray())) }; var result = db.Database.SqlQuery<tb_test>(sql, paramList.ToArray()); foreach (var item in result) { Console.WriteLine(item.id + "," + item.name); } }