【问题标题】:LinQ Error linq joint type inference failed to call 'join' errorLinQ 错误 linq 关节类型推断未能调用“加入”错误
【发布时间】:2013-04-18 05:40:38
【问题描述】:

我对实体框架很陌生,我正在尝试在两个实体上使用 join 子句,如下所示。

var alertlist = from elogAlert  in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert
                        where elogAlert.No_ != null
                        join elogAlertDetail in  yangkeeDBEntity. Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details
                        on elogAlert.No_ == elogAlertDetail.AlertID



                        where elogalertdetail.employee_id == driverid
                        select new
                        {
                            elogalertdetail.employee_id,
                            elogalertdetail.alert_id,
                            elogalertdetail.no_,
                            elogalertdetail.status,
                            elogalertdetail.created_by,
                            elogalertdetail.date_created,



                        };

您好,从上面的代码中,我收到两个错误提示

'Error  1   The name 'elogAlertDetail' is not in scope on the left side of 'equals'.  Consider swapping the expressions on either side of 'equals'.' and 'linq joint type inference failed to call 'join' error  '

目前这两个表没有任何数据。如果有人可以帮助我解决这种情况,我会很高兴

【问题讨论】:

  • AlertIDNo_ 有可能是两种不同的数据类型吗?
  • 同样的数据类型 :) 谢谢
  • 不客气,先生。
  • 嘿,别叫我先生 :) 欢呼声刚刚通过了你的linkedin,我刚刚从 Python / MongoDB 转移到 .net 的东西,更像是颠倒的 :)

标签: c# linq entity-framework


【解决方案1】:

加入 Linq 时不能使用==。您需要使用equals

注意不是method.Equals(..),而是keyword

from elogAlert  in yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert
join elogAlertDetail in  yangkeeDBEntity.Yang_Kee_Logistics_Pte_Ltd_ELog_Tablet_Alert_Details

on elogAlert.No_ equals elogAlertDetail.AlertID //this line has equals instead of ==

                        where elogAlert.No_ != null
                        where elogalertdetail.employee_id == driverid
                        select new
                        {
                            elogalertdetail.employee_id,
                            elogalertdetail.alert_id,
                            elogalertdetail.no_,
                            elogalertdetail.status,
                            elogalertdetail.created_by,
                            elogalertdetail.date_created,
                        };

documentaion上的Linq join

【讨论】:

  • equals 不能用于 int 值,您的答案与这里无关,谢谢
  • @Kalanamith 你是什么意思?当然,您使用equals,事实上您在加入 Linq 时必须这样做。请参阅我的答案中的链接。我还发表了评论,向您展示我在哪里更改了您的代码
【解决方案2】:

您遇到的错误与连接时等于操作数周围的参数顺序有关。

连接的表必须是等号的右轴,左轴必须在您要连接的行中。

在这种情况下,yangkeeDBEntity 不在elogAlert 行中

CF MSDN 中的例子

from c in categories 
        join p in products on c equals p.Category into ps 
        from p in ps 
        select new { Category = c, p.ProductName }; 

c 在您要加入的行中,p.category 在您要加入的表中

另外你还需要使用equals这个词而不是上面提到的==

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多