takiTamNick takiTamNick - 1 year ago 70
SQL Question

Rewriting SQL to LINQ

I'm completely new to LINQ, i want to rewrite some of mine SQL querys into LINQ (just to learn) and i'v already stuck at the beginning.

Here is SQL query :

declare @typMon varchar(200)
set @typMon = 'moneta'

select *
from [db1].[dbo].[Picking]
where number = 1000
and Group IN (Select grupa
from [db2].[dbo].[groups]
where typ = @typMon)

Subquery returns 3 output rows : http://i.stack.imgur.com/CDOwr.png

And here is what i'v write in LINQ

This part works ok :

var query = from x in db.grupyTowarowes
where x.typ == typMoneta
select new

Problem is here:

var test = from z in dbContext.PICKINGs
where z.Number == 1000
&& z.group IN output from 1st query
select new

Answer Source
var test = from z in dbContext.TBL_CSV_PICKINGs 
           join g in db.grupyTowarowes on Z.group equals g.grupa
           where z.Number == 1000 && 
           g.typ == typMoneta
           select new { z.id };

Or in the Method syntax

    .Where(z => z.Number == 1000)
    .Join(db.grupyTowarowes.Where(g => g.typ == typMoneta)
        z => z.group,
        g => g.grupa,
        (z,g) => z.id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download