Burjua
Burjua

Reputation: 12706

How to write a Linq to SQL join query with parameter which returns columns from both table?

I can't figure out how to write this query in Linq:

select ic.icCategory, d.domHosting, d.domCode  
  from Domains d join Image_Categories ic 
    on ( d.domCode = ic.icDomainCode )
      where d.domCode = 'code'

Can anyone help?

Thanks

Upvotes: 0

Views: 2459

Answers (3)

Pleun
Pleun

Reputation: 8920

if you have proper foreign key constraints in your database, there is no need to do all the plumbing on the join yourself. linq-sql creates these also in your classes! That is the beauty of an orm, otherwise you are just writing sql in a different syntax.

You can do

Var x = from i in db.Image_categoriees
    Where i.domain.domcode == 'code'
    Select new { i.icCategory, i.domain.domcode, i.domain.domhosting}

Upvotes: 2

Kiley Naro
Kiley Naro

Reputation: 1769

Try something like this:

var result =
    from d in dbContext.Domains
    join ic in dbContext.Image_Categories on d.domCode equals ic.icDomainCode
    where d.domCode == 'code'
    select new { ic.icCategory, d.domHosting, d.domCode }

There is some more information about the Query Expression Syntax Join Operators and some examples here:

http://msdn.microsoft.com/en-us/library/bb896266.aspx

Upvotes: 1

Mike Mooney
Mike Mooney

Reputation: 11989

from Domains d join Image_Categories ic  on d.domCode equals ic.icDomainCode
where d.domCode = 'code'
select ic.icCategory, d.domHosting, d.domCode  

Upvotes: 0

Related Questions