简体   繁体   English

如何将sp加入表Linq

[英]How to join sp to table Linq

Im using EF (edmx not code first) and i want to join a sp to a 'normal' table so i use the following linq query: 我正在使用EF(edmx不是首先编写代码),并且我想将sp加入“正常”表,因此我使用以下linq查询:

var test = (from obj1 in e.myTable
            join obj2 in e.MySp(aString) on obj1.AStringProperty equals obj2.AStringProperty
            select r.description).ToList();

Which then throws a NotsupportedException with the following message: 然后,它抛出带有以下消息的NotsupportedException:

Unable to create a constant value of type 'Api.DataAccess.Contexts.MySp_Result'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

But all the property's I'm using to join are a string so the only thing I can assume is that the fact that the generated object returned by the sp has an int? 但是我要用来连接的所有属性都是一个字符串,因此我唯一可以假设的是sp返回的生成对象具有int?的事实int? and a long? long? causes this error? 导致此错误? If so how could I get around this? 如果是这样,我该如何解决?

Thanks 谢谢

您不能使用SQL中的存储过程(linq生成SQL)来联接,而是使用用户定义的函数。

can You try this: 你可以试试这个吗?

var test = (from obj1 in e.myTable
            from obj2 in e.MySp(aString) 
            where obj1.AStringProperty == obj2.AStringProperty
            select r.description).ToList();

This is not possible in EF as well as in SQL. 在EF和SQL中这是不可能的。 Linq-to-entities query is translated to SQL. Linq-to-entities查询被转换为SQL。 You can't join result set from SP to any other query. 您不能将SP的结果集加入任何其他查询。

The only possible way is: 唯一可能的方法是:

var test = (from obj1 in e.myTable.ToList()             
            join obj2 in e.MySp(aString) on obj1.AStringProperty equals obj2.AStringProperty             
            select r.description).ToList(); 

But it will be executed as Linq-To-Objects so the whole content of myTable will be transfered to application as well as the whole result set from MySp and join will be performed in memory. 但是它将作为Linq-To-Objects执行,因此myTable的全部内容将传输到应用程序,而MySp和join的全部结果集将在内存中执行。

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

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