Jason W Jason W - 1 month ago 5
C# Question

LINQ Joining Multiple Tables - No Results

I am trying to use LINQ to join multiple tables, and retrieve the name of a distribution method. I have a SQL statement I used as a basis, which follows.

select dt.NAME
from i_doc.dbo.document as d
join i_doc.dbo.CONTACT as c
on c.DOC_ID = d.DOC_ID
join i_doc.dbo.RECIPIENT as r
on r.CONTACT_ID = c.CONTACT_ID
join i_doc.dbo.DISTRIBUTION_TYPE as dt
on dt.DISTRIBUTION_TYPE_ID = r.DISTRIBUTION_TYPE_ID
where c.CONTACT_TYPE_ID = 2
and r.DISTRIBUTION_TYPE_ID != 6
and d.DOC_ID = 28757


This particular query returns 1 item. However, the LINQ code I have returns nothing. The LINQ code follows.

var distributionMethod = (from d in _documentEntities.DOCUMENTs
join c in _documentEntities.CONTACTs on d.DOC_ID equals c.DOC_ID
join r in _documentEntities.RECIPIENTs on c.CONTACT_ID equals r.CONTACT_ID
join dt in _documentEntities.DISTRIBUTION_TYPE on r.DISTRIBUTION_TYPE_ID equals dt.DISTRIBUTION_TYPE_ID
where c.CONTACT_ID == 2
&& r.DISTRIBUTION_TYPE_ID != 6
&& d.DOC_ID == DocID
select dt.NAME).ToList();


When trying to determine what was going on we simplified the query to test if the connection was being made correctly, and tested the following LINQ.

var test = (from dt in _documentEntities.DISTRIBUTION_TYPE
select dt.NAME).Take(100);


This LINQ, var test, results in the appropriate values.

My question is: Why is my distributionMethod LINQ query not returning anything, and what can I do to fix it?

Thank You

Evk Evk
Answer

In your LINQ query you filter by c.CONTACT_ID, but in SQL query you filter by c.CONTACT_TYPE_ID, so that's just simple typo.

Comments