linq to entities, a where in where clause? (inner where)

后端 未结 3 1311
礼貌的吻别
礼貌的吻别 2020-12-16 00:39

I have a table with a one to many mapping to a table that has a many to many mapping to another table. I\'d like to do the following:

var results = context.m         


        
相关标签:
3条回答
  • 2020-12-16 01:13
    from link in db.main_link_table
    join s in db.some_table on link.association1 = s.association
    join m in db.many_to_many_table on link.association2 = m.association
    where s.X = 'MyValue' AND m.Y = 'MyValue'
    select m; // or s or link or both 3 as you want
    
    0 讨论(0)
  • 2020-12-16 01:14

    If you want to do a join, why don't you just do a join?

    var query = from main in context.MainLinks
                join t1 in context.Some on main.Association equals t1.Association
                where t1.RandomProperty == "MyValue"
                join t2 in context.ManyToMany on t1.Association equals t2.Association
                where t2.RandomProperty == "MyValue"
                select new { main, t1, t2 };
    

    That should achieve exactly what your SQL does...

    0 讨论(0)
  • 2020-12-16 01:21

    It should work if you change the inner Where to Any:

    var results = context.main_link_table
                         .Where(l => l.some_table.RandomProperty == "myValue" &&
                                     l.some_table.many_to_many_table
                                      .Any(m => m.RandomProperty == "myValue"));
    
    0 讨论(0)
提交回复
热议问题