问题描述
我有一个想要获取的对象ID的列表。列表可能很大
context.MyObject.Where(obj=> myIdList.Contains(obj.Id))
。可能有超过10000个条目。
解决方案应该是创建一个临时表,例如:Create temporary list/table in SqlServer。我可以在SQL中使用这些值,然后通过联接选择所需的行。看起来像这样:
declare @idTable table (Id int)
insert into @idTable values (1),(2),(5),(7),(10)....
select MyTable.* from MyTable
inner join @idTable as ids on ids.Id= MyTable.Id
使用ef core可以达到类似的效果,如here:
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Id",typeof(int)));
foreach (int id in myIdList)
{
DataRow row = table.NewRow();
row["Id"] = v.FieldId;
table.Rows.Add(row);
}
var param = new SqlParameter("@idTable",table) { TypeName = "dbo.CustomSqlType",SqlDbType = SqlDbType.Structured };
var myObjects = context.MyObject.FromRawSql("select MyTable.* from MyTable
inner join @idTable as ids on ids.Id= MyTable.Id",param);
但是要使其在ef-core中起作用,我需要用户定义的表类型。由于数据库的性质,我宁愿不这样做。这个-> TypeName = "dbo.CustomSqlType"
丢失了,我无法在服务器上创建它。并且上面的sql显示没有可能。
创建用户定义表类型的另一种方法是每次执行时都创建一个大的sql字符串。出于安全原因,我不愿意这样做(我不相信自己可以转义,并且我不应该),并且我希望能够重用代码,以便可以通过idLists从其他表中检索对象。
可以在ef-core中以可重用的方式在没有用户定义表类型的情况下做我想做的事吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)