C# 使用Dapper作为in子句的参数传入项列表(可能为null)

C# 使用Dapper作为in子句的参数传入项列表(可能为null),c#,sql,sql-server,sql-server-2008,dapper,C#,Sql,Sql Server,Sql Server 2008,Dapper,当我试图使用Dapper作为参数传递一个空的项目列表时,我得到了一个空引用异常。通常,在where子句中,我只需执行以下操作: "AND (@Sections IS NULL OR Section IN @Sections)"; var parameters = new { ApplySectionFilter = stringsToFilter != null, Sections = stringsToF

当我试图使用Dapper作为参数传递一个空的项目列表时,我得到了一个空引用异常。通常,在where子句中,我只需执行以下操作:

"AND (@Sections IS NULL OR Section IN @Sections)";
var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
但我无法做到这一点,因为即使部分列表中有项目,它也无法工作。Dapper将它们作为参数添加,并且(@sections1、@sections2为NULL或)将出错。如果因为不想将分区列表用作筛选器而将其保留为空,则会出现空引用异常

var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
我的函数必须有一个节列表作为可选参数。通过这种方式,在我的代码中,我不必总是向查询中添加节过滤器。如何在函数参数中使节成为可为空的列表,但在为空时也可以使用Dapper

var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
这是我的代码:

public static IEnumerable<Product> GetProformaFormularies(int? categoryId = null, IEnumerable<int> sections = null)
{
    using (var context = new AppContext())
    {
        var sql =
        "SELECT * " +
        "FROM Products " +
        "WHERE (@CategoryId IS NULL OR CategoryId = @CategoryId) " +
          "AND (Section IN @Sections)";

        return context.Database.Connection.Query<Product>(sql, 
        new { 
                CategoryId = categoryId,
                Sections = sections
            }).ToList();
    }
}
var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };

如果
sections
null
,则可以省略
WHERE
子句的该部分:

var sql =
    "SELECT * " +
    "FROM Products " +
    "WHERE (@CategoryId IS NULL OR CategoryId = @CategoryId) ";

if (sections != null)
{
    sql += "AND (Section IN @Sections)"
}

return context.Database.Connection.Query<Product>(sql, 
    new { 
            CategoryId = categoryId,
            Sections = sections
        }).ToList();
var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
var-sql=
“选择*”+
“来自产品”+
其中(@CategoryId为NULL或CategoryId=@CategoryId)”;
如果(节数!=null)
{
sql+=“和(在@Sections中的节)”
}
返回context.Database.Connection.Query(sql、,
新{
CategoryId=CategoryId,
截面=截面
}).ToList();

看起来dapper只会忽略你传递的对象上的
部分
属性,如果它不适用的话。

在我看来,代码中的if语句有时不是你想要的。有时我只是执行
.sql
文件。所以对我来说唯一有效的就是这个

var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
有一点开销,但最后,它更干净。在我看来

var parameters = new
            {
                ApplySectionFilter = stringsToFilter != null,
                Sections = stringsToFilter 
            };
SELECT * FROM Products
    WHERE (@ApplySectionFilter = 0 OR Section IN @Sections)

难以置信的非常感谢你。请查看我刚刚更新的帖子,以及我刚刚提出的解决方案。马修彼得森:没问题!如果不需要传递额外的“Sections”属性,那么您的解决方案也很好