我对LINQ完全陌生,我想把我的一些SQL查询重写到LINQ中(只是为了学习),但我一开始就被卡住了。
以下是SQL查询:
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)
子查询返回3个输出行:https://i.stack.imgur.com/CDOwr.png
这是我在LINQ 中写的内容
这部分工作正常:
var query = from x in db.grupyTowarowes
where x.typ == typMoneta
select new
{
x.grupa
};
问题就在这里:
var test = from z in dbContext.PICKINGs
where z.Number == 1000
&& z.group IN output from 1st query
select new
{
z.id
};
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 };
或者在方法语法中
dbContext.TBL_CSV_PICKINGs
.Where(z => z.Number == 1000)
.Join(db.grupyTowarowes.Where(g => g.typ == typMoneta)
z => z.group,
g => g.grupa,
(z,g) => z.id);