简体   繁体   English

UDT作为EF4查询中的参数

[英]UDT as a parameter in EF4 query

I know that EF4 does not support User Defined Table Types (yet). 我知道EF4不支持用户定义的类型(尚未)。 I need to write a query, that accepts a list of pairs <product_code, quantity> and returns a record set with product_code and price for each product_code passed, based on quantity . 我需要编写一个查询,接受对列表<product_code, quantity>并返回一个记录集product_codeprice为每product_code过去了,基于quantity What is my best option with EF4 to model this? EF4的最佳选择是什么? The calculation in the database to get the price are quite complex, and there are a lot of products, which means that most of the action should happen server-side. 在数据库中计算得到的价格相当复杂,并且有很多产品,这意味着大多数操作都应该在服务器端发生。 (For example I can't get full list of prices from server first and then filter to the products I need on the client. I also can't apply quantity to the calculation on the client, that has to be passed to server and processed there). (例如,我无法首先从服务器获取完整的价格列表,然后过滤到我在客户端上需要的产品。我也无法将数量应用于客户端上的计算,必须传递给服务器并进行处理那里)。 Any thoughts are welcome. 欢迎任何想法。

I think you mostly answered your question. 我想你大多回答了你的问题。 Computation must be done on the database server and you just want to get result, don't you? 计算必须在数据库服务器上完成,你只想得到结果,不是吗? If you are using SQL Server 2008 you can create stored procedure which accepts table valued parameter . 如果您使用的是SQL Server 2008,则可以创建接受表值参数的存储过程。 Now you can call this procedure either directly using ADO.NET or using EF and context.ExecuteStoreQuery where you still pass DataTable to SqlParameter with SqlDbType.Structured . 现在,您可以直接使用ADO.NET或使用EF和context.ExecuteStoreQuery调用此过程,您仍然可以使用SqlDbType.StructuredDataTable传递给SqlParameter

If you don't use SQL Server 2008 you need stored procedure with one big nvarchar parameter passing whole list as comma delimited string. 如果您不使用SQL Server 2008,则需要存储过程,其中一个大的nvarchar参数将整个列表作为逗号分隔的字符串传递。 Your stored procedure will first parse this list to temporary table and then process the computation in the same way as with table valued parameter. 您的存储过程将首先将此列表解析为临时表,然后以与表值参数相同的方式处理计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM