首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

entity增添where条件查询

2013-06-26 
entity添加where条件查询dbQuery dbQuery.Include(ReturnReportKeys.ReturnReport)像这样的include生

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()

热点排行