简体   繁体   English

LINQ to SQL加入问题

[英]LINQ to SQL Join issues

I'm trying to use the following LINQ to SQL in my code: 我正在尝试在我的代码中使用以下LINQ to SQL:

  (from s in dc.Accounts 
  join purchases in dc.Transactions on s.AccID equals purchases.Account into pu
  join pop in dc.POPTransactions on new { s.ID, syncNo } equals new {AccId = pop.AccountID, SyncNo = pop.SyncNo } into po
  where s.AccID == ID && s.Customer == false
  select new AccsandPurchase { acc = s, purchases = pu.ToList(), pop = po.ToList() } ));

The error happens on the second join line (3rd line in the whole query above) - I used to have it so it just joined on s.ID and pop.AccountID and that worked perfect, but now I introduced another join critieria (the syncno) I get the following error: 错误发生在第二个连接线上(上面整个查询中的第3行) - 我曾经拥有它所以它刚刚加入了s.ID和pop.AccountID并且工作得很完美,但现在我引入了另一个连接标准(syncno) )我收到以下错误:

"The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'" “join子句中某个表达式的类型不正确。在调用'GroupJoin'时类型推断失败”

Any ideas? 有任何想法吗? Some notes: 一些说明:

1: 'the variable 'syncNo' is a long, as is the value in the DB (bigint). 1:'变量'syncNo'是一个long,就像DB(bigint)中的值一样。 The value in the db is nullable so I've also tried "long?" db中的值是可以为空的,所以我也尝试过“long?” as the variable type 作为变量类型

2: AccsandPurchase is a custom class I made, as you can probably guess 2:AccsandPurchase是我制作的自定义课程,你可能猜到了

Thanks 谢谢

尝试指定相同的连接键名称,例如

join pop in dc.POPTransactions on new { Key1 = s.ID, Key2 = syncNo } equals new {Key1 = pop.AccountID, Key2 = pop.SyncNo }

From the MSDN docs: 来自MSDN文档:

Type inference on composite keys depends on the names of the properties in the keys, and the order in which they occur. 复合键的类型推断取决于键中属性的名称以及它们出现的顺序 If the properties in the source sequences do not have the same names, you must assign new names in the keys. 如果源序列中的属性不具有相同的名称,则必须在键中指定新名称。 For example, if the Orders table and OrderDetails table each used different names for their columns, you could create composite keys by assigning identical names in the anonymous types: 例如,如果Orders表和OrderDetails表各自为其列使用不同的名称,则可以通过在匿名类型中指定相同的名称来创建复合键:

join...on new {Name = o.CustomerName, ID = o.CustID} equals 
   new {Name = d.CustName, ID = d.CustID }

http://msdn.microsoft.com/en-us/library/bb907099.aspx http://msdn.microsoft.com/en-us/library/bb907099.aspx

This problem often occurs when you are comparing the properties of different types as well. 当您比较不同类型的属性时,通常会发生此问题。 Such as comparing a short to an int, or a string to an int, etc. The names must match, but when your comparison already has equal names and you can't find the issue, check if they are also of the same type. 例如将short与int或字符串与int等进行比较。名称必须匹配,但是当您的比较已经具有相同的名称而您无法找到问题时,请检查它们是否也是同一类型。

from p in DbSet.AsQueryable()
join t in _dbContext.SomeEntity on new { p.Id, Type = (int)MyEnum.Something }
                            equals new { t.Id, Type = t.EntityType}

In this example, if t.EntityType is short , which is being compared to an integer , it will also give you the message: 在此示例中,如果t.EntityType为short (与整数进行比较),它还会为您提供以下消息:

"The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'" “join子句中某个表达式的类型不正确。在调用'GroupJoin'时类型推断失败”

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

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