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)
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 a bit confusing for me. I know that left outer joins aren't built in to LINQ natively and that you have to use 'into' and 'DefaultIfEmpty()', but I have a bit of a complex SQL query.
The query:
SELECT * FROM TableA as a
LEFT OUTER JOIN TableB as b
on a.ID = b.ID and a.StatusOne = 1 AND b.StatusOne = 1 AND (a.StatusTwo != 1 OR b.StatusTwo!= 1)
LEFT OUTER JOIN TableC as c
on a.ID = c.ID AND a.StatusOne = 1 AND c.StatusOne = 1 AND (a.StatusTwo != 1 OR c.StatusTwo != 1)
WHERE
a.ID = 99999 AND (b.ID is not null OR c.ID is not null)
I'm not even really sure where to begin on this. If someone could help me out I would appreciate it immensely.
I am giving you the idea behind, I didn't tested the code, but to join two tables based on different fields, your join should have a anonymous type to compare.
var one = 1
from a in tableA
join b in tableB on new { a.ID, b.StatusOne } equals new { b.ID, one} into ab
But in your case you have more than one condition, so before calling DefaultIsEmpty, you should check the latest condition
from a in tableA
join b in tableB on new { a.ID, b.StatusOne } equals new { b.ID, one} into ab
from ab in ab.Where(x => x.a.StatusTwo != 1 || x.b.StatusTwo != one).DefaultIfEmpty()
Then the other outer join follows the same pattern and you need to make a final join. A good way to start is downloading the LinqPad and see which lambda expression it generates for your query, and you can take it from there. You can optimize it later but you will get the idea behind the generation. Hope this helps
This question already has answers here:
LEFT OUTER JOIN in LINQ
(24 answers)
Closed 8 years ago.
I am new to Linq, and I need to convert this query to a left outer join between DocumentStores and Orders, as not all documents are related to an order:
var documents=(from d in _dataContextOrders .DocumentStores
join o in _dataContextOrders.Orders on d.OrderID equals o.ID
join t in _dataContextOrders .DocumentTypes on d.DocumentType equals t.DocTypeID
select new
{
d.ID,
o.PORef ,
t.DocTypeDescription,
d.Name,
d.ContentType
}).ToList();
How do I achieve this?
from a in dataContext.<tableA>
join _b in dataContext.<tableB> on a.id equals _b.aid into _b
from b in _b.DefaultIfEmpty()
select <whatyouwanttoselect>
b will be null, if the join on the ids fails
This question already has answers here:
LINQ - Full Outer Join
(16 answers)
Closed 8 years ago.
I have 3 entities in EntityModel: Customer, CustomerAddress (this is junction entity with Ids and orderOnControl columns and cant be ommited in model) and Address. I need to make linq query to FULL JOIN Customer with Address. This query in SQL is plain:
select e1.[AddressID], e1.City, e3.CustomerID, e3.LastName from [SalesLT].[Address] as e1
full join [SalesLT].[CustomerAddress] as e2 on e1.[AddressID] = e2.[AddressID]
full join [SalesLT].[Customer] as e3 on e2.CustomerID = e3.CustomerID
but I need to write this with linq, I found answers where there are 2 entities with many-to-may relation, but couldn't find any with junction entity, I would appreciate any tips
Give this a shot. This LINQ statement will join Customers to Addresses through the junction table CustomerAddress.
var query = from ca in context.CustomerAddress
join a in context.Address on ca.AddressId equals a.AddressId
join c in context.Customer on ca.CustomerId equals c.CustomerId
select new { a.AddressId, a.City, c.CustomerId, c.LastName };
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.