日期:2014-05-20  浏览次数:20692 次

entity添加where条件查询
dbQuery = dbQuery.Include("ReturnReportKeys.ReturnReport");
像这样的include生成的语句是inner join 两张表,那么如何让这个生成的inner join在查询的时候多一个条件查询呢?
表结构如下:
 A表
 -------
 a
 
B表
 --------
 a  c
 
C表
 --------
 c  d
 主要是在entity里面有这样的关系:
 假设A,B,C表对应的entity为A,B,C
 1.A中有:Icollection<B> b{get;set;}
 2.B中有:C c{get;set;}
entity?include

------解决方案--------------------
var query=from a in db.TableA 
          join b in db.TableB on a.id equals b.Aid
          where b.name=="Tim"
          join c in db.TableC on b.id equals c.Bid
          where c.Age==33
          select new {a,b,c};

------解决方案--------------------
a better way, and I think it's cleaner, will be


var query = 
    from a in db.TableA
    from b in db.TableB.Where(i=>i.PK == a.FK)
    from c in db.TableC.Where(i=>i.PK1 == b.FK1)
    where c.Age == 33 && b.name == "Tim"
    select a;           


it's less wordy, and easy to read 

FYI, they both generate the exact same SQL, if using ToTraceString()