只是想知道如何用LINQ语法编写以下SQL
select * from COESDetails
where COESNo in
(select distinct COESNo from COESDefects)
Fluent或Query Expression,或两者
COESDefects
具有COESDetails
public class COESDefects
{
public int Id { get; set; }
public int COESNo { get; set; }
public string Comments { get; set; }
public virtual COESDetails COESDetails { get; set; }
}
public class COESDetails
{
public COESDetails()
{
COESDetailsCOESDefects = new List<COESDefects>();
}
public int COESNo { get; set; }
public int Postcode { get; set; }
public virtual ICollection<COESDefects> COESDetailsCOESDefects { get; set; }
}
由于
答案 0 :(得分:1)
试试这个:
var query =
from x in context.COESDetails
join y in context.COESDefects on x.COESNo equals y.COESNo into ys
where ys.Any()
select x;
答案 1 :(得分:0)
我觉得它会像这样简单:
yourDbContext.COESDetails.Where(detail => detail.COESDetailsCOESDefects.Any()).ToList();
如果我错了,请纠正我。
答案 2 :(得分:0)
有时,如果查询很复杂,直接执行它就会更容易,例如。
IEnumerable<COESDetails> myResults = dbConext.ExecuteQuery<CCOESDetails>("select * from COESDetails where COESNo in (select distinct COESNo from COESDefects)")