【问题标题】:How to join sp to table Linq如何将 sp 加入表 Linq
【发布时间】:2011-02-23 09:37:06
【问题描述】:

我使用的是 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();

然后引发 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.

但是我用来连接的所有属性都是一个字符串,所以我唯一可以假设的是,由 sp 返回的生成对象有一个int? 和一个long? 会导致这个错误?如果是这样,我该如何解决?

谢谢

【问题讨论】:

    标签: c# .net linq entity-framework linq-to-entities


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你可以试试这个:

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

      【讨论】:

      • 看不出这有什么帮助。
      【解决方案3】:

      这在 EF 和 SQL 中都是不可能的。 Linq-to-entities 查询被转换为 SQL。您不能将来自 SP 的结果集连接到任何其他查询。

      唯一可能的方法是:

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

      但它将作为 Linq-To-Objects 执行,因此 myTable 的全部内容将被传输到应用程序以及来自 MySp 的整个结果集和连接将在内存中执行。

      【讨论】:

        猜你喜欢
        • 2014-02-16
        • 1970-01-01
        • 2016-11-21
        • 2021-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-20
        • 1970-01-01
        相关资源
        最近更新 更多