Performing LINQ Self Join

Posted by senfo on Stack Overflow See other posts from Stack Overflow or by senfo
Published on 2011-01-16T03:42:18Z Indexed on 2011/01/16 3:53 UTC
Read the original article Hit count: 177

Filed under:
|
|

I'm not getting the results I want for a query I'm writing in LINQ using the following:

var config = (from ic in repository.Fetch()
              join oc in repository.Fetch() on ic.Slot equals oc.Slot
              where ic.Description == "Input" && oc.Description == "Output"
              select new Config
              {
                  InputOid = ic.Oid,
                  OutputOid = oc.Oid
              }).Distinct();

The following SQL returns 53 rows (which is correct), but the above LINQ returns 96 rows:

SELECT DISTINCT
    ic.Oid AS InputOid,
    oc.Oid AS OutputOid
FROM dbo.Config AS ic
INNER JOIN dbo.Config AS oc ON ic.Slot = oc.Slot
WHERE
    ic.Description = 'Input' AND
    oc.Description = 'Output'

How would I replicate the above SQL in a LINQ query?

Update: I don't think it matters, but I'm working with LINQ to Entities 4.0.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ