首页 > 解决方案 > 使用“匿名”表值参数执行存储查询

问题描述

我正在尝试ObjectContext.ExecuteStoreQuery使用表值 SQL 参数执行

var data = new DataTable();
data.Columns.Add("NAME1", typeof(int));
data.Columns.Add("NAME2", typeof(int));

... add rows to the data table

SqlParameter param = new SqlParameter()
{
    ParameterName = "@pairs",
    Value = data,
    SqlDbType = SqlDbType.Structured
};
ObjectContext.ExecuteStoreQuery<long>(
                "select ID" +
                "from TEST t join @pairs p " +
                "on t.NAME1 = p.NAME1 and t.NAME2= p.NAME2"
)

执行终止并出现以下错误:

System.ArgumentException: '表类型参数'@pairs' 必须具有有效的类型名称。'

我知道我没有指定类型名称,因为这不是数据库中现有的表类型。那么,有没有办法将任何非原始参数的列表传递给查询?还是我错过了什么?

任何使用ObjectSet/Queryable代替本机查询的替代解决方案都将被接受。

标签: sqlsql-serverentity-frameworkrelational-database

解决方案


推荐阅读