像下面这样, 是查不出来的. 是有什么特殊姿势我不会的吗? google 了半天也没找出来怎么传递集合参数进去.
var ids = new List<tring>() { "A001", "B001" }; appDbContext.RackTransfers.FromSql($"select * from RackTransfers where Id IN ({ids})");
![]() | 1 corcre 2024-10-09 16:30:35 +08:00 可能就是不支持的吧 之前上网搜了一下发现没解决方案就放弃了, 现在一般要不就把 ids 给先处理好传进去(处理成('A001','B001')然后字符串拼接) 要不就根据 ids 的长度循环塞进去, 例如..... IN {@id0,@id1,@id2}然后按正常方式传参... 反正都挺麻烦的 |
![]() | 2 jiangzm 2024-10-09 16:49:11 +08:00 EF.Functions.Like 或者 Contains |
![]() | 3 luojianxhlxt 2024-10-09 17:04:06 +08:00 你这是 sql 语句,得用 string.join 拼接成 where id in('A001','A002') |
![]() | 4 liuliuliuliu PRO |
![]() | 5 liuliuliuliu PRO 题外话,不知道你怎么搜索的,怎么会没有相关资料的呢? 谷歌,关键字 entity framework where in ,第一条就是 stackoverflow 上的答案啊 |
6 wu00 2024-10-09 17:21:52 +08:00 appDbContext.RackTransfers.FromSql($"select * from RackTransfers where Id IN ('{string.Join("','", ids)}')"); |
7 bthulu OP @nikenidage1 你这个大家都知道, 但是很多情况下, 没法这样查. 比如取 group by 后组内第一条返回一个集合, 目前 ef core 还不支持这个功能, 官方说至少的 efcore10 才有可能支持. ``` using AppDbContext dbCOntext= CreateAppDbContext(); var queryable = dbContext.Racks.Where(e => e.Occupied == true).GroupBy(e => e.Aisle) .Select(g => g.OrderBy(e => e.Col).Take(1)); // 支持 List<IEnumerable<Rack>> list = queryable.ToList(); // 不支持, 可能 efcore10.0 会支持, 参考 https://github.com/dotnet/efcore/issues/28002 List<Rack> racks = queryable.SelectMany(e => e).ToList(); ``` |
8 bthulu OP @nikenidage1 请注意看题, 使用 FromSql 查询时, 不是使用 Where 查询. |
![]() | 9 Removable 2024-10-09 17:38:27 +08:00 |
![]() | 10 Removable 2024-10-09 17:40:59 +08:00 @Removable #7 可以单独写个静态类去处理字符串/数字类型的拼接,省得每次都要写 string.Join 和 Linq |
11 VxLzKg4uLbi32w60 2024-10-09 17:49:20 +08:00 ![]() OP 用的什么版本?.NET6 现在是这样,可以用参数化进行传参,可以试试 var ids = new List<string>() { "A001", "B001" }; var parameters = new[] { new NhgdbParameter("@Ids ", ids )} }; appDbContext.RackTransfers.FromSqlRaw($"select * from RackTransfers where Id IN @Ids ",parameters ); |
12 VxLzKg4uLbi32w60 2024-10-09 17:51:00 +08:00 @a194259440 如果不行,建议用其他 ORM 框架,Dapper 保底可以实现 |
![]() | 13 liuliuliuliu RO @bthulu 哦用 FromSql 那我理解错了 |
14 bthulu OP @a194259440 在.net8.0+efcore8.0 中实测下面两种方式都不行, 直接报错 System.NotSupportedException 方式一: int[] ids = [1, 2, 3]; List<MySqlParameter> parameters = ids.Select(e => new MySqlParameter("@Ids", e)).ToList(); List<Rack> racks = dbContext.Racks.FromSqlRaw("select * from rack where id in (@Ids)", parameters).ToList(); 方式二: int[] ids = [1, 2, 3]; MySqlParameter parameters = new MySqlParameter("@Ids", ids); List<Rack> racks = dbContext.Racks.FromSqlRaw("select * from rack where id in (@Ids)", parameters).ToList(); |
15 bthulu OP 上面两个示例中, 将 new MySqlParameter("@Ids", ...)改为 new MySqlParameter("Ids", ...), 也是一样的报错 |
![]() | 16 PopRain 2024-10-09 20:25:15 +08:00 in 的参数化查询语句是这样的:where x in (@0,@1,@2..... ) ; 所以,后台需要把 List 转换为一个个的参数,不知道 efcore 是否有这个功能,我们公司是自己实现的 |
![]() | 17 beginor 2024-10-10 09:01:23 +08:00 via Android 不知道楼主使用的是什么数据库,如果是 pg 的话可以直接用数组参数 |
![]() | 18 beginor 2024-10-10 09:36:23 +08:00 楼主用的应该是 mysql , 不支持数组类型,无法使用数组参数, 应该只能按照 16 楼的方法拼接 sql 参数了 |