Jefferson
Jefferson

Reputation: 69

Linq with different data types

I am getting this error The type of one of the expression in the join clause is incorrect Type. I believe its because the different data types. In the Customer table CustomerId is a bigint but in the CustmerGroupXref CustomerId is just an int. Is there a way to get this working in linq? I dont want to change the database field type.

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

enter image description here

enter image description here

Upvotes: 2

Views: 543

Answers (1)

D Stanley
D Stanley

Reputation: 152654

Well you could try a cast:

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

or a conversion:

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

But I would recommend just making the types equal if it's intended to be a foreign key - you run the risk of having IDs in the customer table that can't be referenced in the group table.

Upvotes: 4

Related Questions