1

只是想知道如何用 LINQ 语法编写以下 SQL

select * from COESDetails
where COESNo in
(select distinct COESNo from COESDefects)

流利或查询表达式,或两者

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; }            
}

谢谢

4

3 回答 3

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;
于 2013-09-20T03:51:55.107 回答
0

有时,如果查询很复杂,直接执行它会更容易,例如

IEnumerable<COESDetails> myResults = dbConext.ExecuteQuery<CCOESDetails>("select * from COESDetails where COESNo in (select distinct COESNo from COESDefects)")
于 2013-09-20T03:10:19.773 回答
0

我觉得它会像这样简单:

yourDbContext.COESDetails.Where(detail => detail.COESDetailsCOESDefects.Any()).ToList();

如我错了请纠正我。

于 2013-09-20T02:52:08.650 回答