开发者

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:

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:

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 t开发者_Python百科o 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? and a long? causes this error? If so how could I get around this?

Thanks


You can't join with Stored procedures in SQL (linq generates SQL), use a User defined function instead.


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. Linq-to-entities query is translated to SQL. You can't join result set from SP to any other query.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜