C# 具有不同数据类型的Linq

C# 具有不同数据类型的Linq,c#,linq,C#,Linq,我收到此错误join子句中的一个表达式的类型不正确。我相信这是因为不同的数据类型。在Customer表中,CustomerId是一个bigint,但在customergroupxref中,CustomerId只是一个int。有没有办法让它在linq中工作?我不想更改数据库字段类型 var query1 = (from c in db.Customers join cgx in db.CustomerGroupXrefs on c.CustomerId equal

我收到此错误
join子句中的一个表达式的类型不正确
。我相信这是因为不同的数据类型。在Customer表中,CustomerId是一个bigint,但在customergroupxref中,CustomerId只是一个int。有没有办法让它在linq中工作?我不想更改数据库字段类型

var query1 =  (from c in db.Customers
               join cgx in db.CustomerGroupXrefs on c.CustomerId  equals cgx.CustomerId)


你可以试试演员阵容:

var query1 =  (from c in db.Customers
               join cgx in db.CustomerGroupXrefs 
                   on c.CustomerId equals (long)cgx.CustomerId)
或转换:

var query1 =  (from c in db.Customers
               join cgx in db.CustomerGroupXrefs 
                   on c.CustomerId equals Convert.ToInt64(cgx.CustomerId))

但是,如果要将类型设置为外键,我建议将其设置为相等的类型—您可能会在customer表中遇到无法在group表中引用的ID。

为什么不更改类型?当您想要引用ID对于
int
来说太大的客户时会发生什么?我只是不想在此时更改数据库,只是想知道是否有其他方法可以这样做为什么客户ID的大小不同?也许您应该将客户ID设置为字符串而不是整数,这样您就不会有问题。