如何将流动的 sql 查询转换为 linq,以便我可以在我的 web api 控制器中使用。
select * from String where StringNumber in
(
select Str1 from Detail where DetailNr = 6
union
select Str2 from Detail where DetailNr = 6
union
select Str3 from Detail where DetailNr = 6
union
select Str4 from Detail where DetailNr = 6
)
我试图选择单独的字符串编号,我想出了这个:
var str1 = _dataContext.Context.Detail.AsQueryable()
.Where(n => n.DetailNr == 6)
.Select(n => n.Str1 );
var str2 = _dataContext.Context.Detail.AsQueryable()
.Where(n => n.DetailNr == 6)
.Select(n => n.Str2 );
var str3 = _dataContext.Context.Detail.AsQueryable()
.Where(n => n.DetailNr == 6)
.Select(n => n.Str3 );
var str4 = _dataContext.Context.Detail.AsQueryable()
.Where(n => n.DetailNr == 6)
.Select(n => n.Str4 );
然后是这样的:
var result= _dataContext.Context.String.AsQueryable()
.Where(s => s.StringNumber == str1 or str2 or str2 or str4).ToList();
这应该如何写在 asp.net MVC 控制器中?也许使用 SelectMany 子句?