This question already has answers here:
The type of one of the expressions in the join clause is incorrect in Entity Framework
(4 answers)
Closed 8 years ago.
I'm trying to do this search below, but I'm receiving an error.
ViewBag.IDCONCESSAO =
from p in db.SINCO_CONCESSAO.ToList()
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.NOME_MUNICIPIO
select new
{
Id = p.IDCONCESSAO,
Nome = p.IDCONCESSAO + " - " + c.NOME_MUNICIPIO
};
Error:
The type of one of the expressions in the join clause is incorrect.
Type inference failed in the call to 'Join'.
What is incorrect?
The error was in that line:
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.NOME_MUNICIPIO
I was doing a relation with different types, because IDMUNICIPIO is int and NOME_MUNICIPIO is string. When I changed to this, it worked:
join c in db.MUNICIPIOS_VIEW.ToList() on p.IDMUNICIPIO equals c.ID_MUNICIPIO
Related
This question already has answers here:
The type of one of the expressions in the join clause is incorrect in Entity Framework
(4 answers)
Closed 1 year ago.
If this question has already been asked then please direct me to the SO link so I may avoid a dupe. But I wasn't able to find anything that directly answered the question.
I have the following scenario
var result = (from a in table1
join b in table2
on a.column equals b.column
join c in table3
on b.column equals c.column
join d in table4
on new { a.column2, c.column2 }
equals { d.column3, d.column6 }
select a);
The problem is that column2 of table3 and column6 of table4 are both of type int but 1 is nullable and the other isn't which results in "type of one of the expressions in the join clause is incorrect"
I understand the error but my question; is there any way around this? Or am I just at a dead end attempting to achieve the result I need using linq?
I've attempted to make the join work by using the Value attribute of the nullable type but that doesn't work. If there is no other recourse to achieve this using linq then I'll have to move all the code to SQL in a SPROC which I'd prefer not to do.
I have noted that I can perform the join outside of the anonymous declarations and linq has no issues with it; but I need a composite join.
Thanks
You need to give the properties in the anonymous type labels and the labels must be the same. Then, you can cast the non-nullable int to a nullable int.
var result = (from a in table1
join b in table2
on a.column equals b.column
join c in table3
on b.column equals c.column
join d in table4
on new { JoinColumn1 = a.column2, JoinColumn2 = (int?)c.column2 }
equals { JoinColumn1 = d.column3, JoinColumn2 = d.column6 }
select a);
This is my code:
// Put together a list of new communities
var communityList = from x in db.CommunityTeams
join y in db.Communities on x.CommunityId equals y.CommunityId
join z in db.CommunityRoles on x.CommunityRole equals z.Role
where x.UserId == userId
select new
{
CommunityName = y.ComunityName,
CommunityRoleName = z.Role
};
The join z in db.CommunityRoles is giving me this error:
the Join clause in incorrect. How do I get the syntax correct?
Your syntax isn't incorrect. The tables you are joining with columns x.CommunityRole and z.Role are not in the same type.
Error CS1941 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
You have probably getting this error and you have to join columns with same types for example int and int. Check both of them must be same.
This question already has answers here:
Linq to Entity Join table with multiple OR conditions
(3 answers)
Closed 6 years ago.
So what I'm trying to do is :-
SELECT * FROM TableA
JOIN TableB ON TableA.OriginPhoneNumber=TableB.Id OR TableA.DestinationPhoneNumber=TableB.Id
Rather strange query I know! But I'm trying to replicate this in EntityFramework/Linq - looking at all the samples I can see a pretty easy way to do it when the join is using an AND (using anonymous types) but does the same result exist for a OR join?
Just do a cross join with a where clause
var results = from a in db.TableA
from b in db.TableB
where a.OriginPhonenumber == b.Id
|| a.DestinationPhoneNumber == b.Id
select new { A = a, B = b };
It's doubtful that an or in a join condition would be more efficient than this, but it's likely that either would result in the same execution plan. Performance aside it will give the same results.
I used Union
var firstJoin=from tbl in TableA
join c in TableB
on c.OriginPhoneNumber Equals tbl.Id
var secondJoin=from tbl in TableA
join c in TableB
on c.DestinationPhoneNumber Equals tbl.Id
var result=firstJoin.Union(secondJoin)
This question already has answers here:
linq join and count
(2 answers)
Closed 7 years ago.
I have a situation where I want to count the total transaction of the customer. I can do it in a mysql query but I don't know to do it in linq sql.
select c.name, count(r.custid)
from receipt r left join in cust c on (c.custid=r.custid)
group by r.custid
How can I convert it in linq sql?
I wouldn't start with the SQL... I'd start by thinking of what you're counting, which is basically the number of receipt records in the group which matches the customer. It sounds like you want something like:
var query = from customer in db.Customers
join receipt in db.Receipts
on customer.CustomerId equals receipt.CustomerId
into customerReceipts
select new { customer.Name, Count = customerReceipts.Count() };
Note that unlike many "left join" examples in LINQ, we don't use customerReceipts.DefaultIfEmpty() because we only want the count - which should be 0 if there are no receipts for that customer.
This question already has an answer here:
What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'
(1 answer)
Closed 8 years ago.
I am trying to join 3 tables using LINQ from 2 different SQL Servers (entities).
Error: The specified Linq expression contains references to queries that are associated with different contexts
var query = from a in EntityA.TableA
join p in EntityA.TableB
on a.PersonID equals p.PersonID
join m in EntityB.TableC
on Convert.ToInt32(a.SourceID) equals m.ID
where p.someID == "100000527"
select m.ID;
Please help me to resolve this.
Answer:
var query = from a in EntityA.TableA
join p in EntityA.TableB
on a.PersonID equals p.PersonID
where p.someID == "100000527"
select a.ID;
IQueryable<int> ID = null;
foreach (var item in query)
{
int sourceID= Convert.ToInt32(item);
ID = (from m in EntityB.TableC
where m.ID == sourceID
select m.ID).Distinct();
}
return ID;
Is this right approach?
You can only write Linq to SQL Queries on one database at a time.
If you want to join the data together, you will have to write the two queries separately, create anonymous type objects from them, then join them together using plain old Linq on objects.