使用Dapper ORM从X WHERE ID IN(...)中选择* [英] SELECT * FROM X WHERE id IN (...) with Dapper ORM

查看:125
本文介绍了使用Dapper ORM从X WHERE ID IN(...)中选择*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当IN子句的值列表来自业务逻辑时,使用Dapper ORM使用IN子句编写查询的最佳方法是什么?例如,假设我有一个查询:

What is the best way to write a query with IN clause using Dapper ORM when the list of values for the IN clause is coming from business logic? For example let's say I have a query:

SELECT * 
  FROM SomeTable 
 WHERE id IN (commaSeparatedListOfIDs)

The commaSeparatedListOfIDs 是从业务逻辑传递过来的,它可以是 IEnumerable(Integer)的任何类型。在这种情况下,我将如何构造查询?

The commaSeparatedListOfIDs is being passed in from business logic and it can be any type of IEnumerable(of Integer). How would I construct a query in this case? Do I have to do what I've been doing so far which is basically string concatenation or is there some sort of advanced parameter mapping technique that I'm not aware of?

推荐答案

Dapper直接支持此功能。例如...

Dapper supports this directly. For example...

string sql = "SELECT * FROM SomeTable WHERE id IN @ids"
var results = conn.Query(sql, new { ids = new[] { 1, 2, 3, 4, 5 }});

这篇关于使用Dapper ORM从X WHERE ID IN(...)中选择*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆