【转】Entity Framework 学习积累

转自:http://www.cnblogs.com/mbailing/archive/2012/07/16/2593368.html

EF4.1/4.3 的一些使用小技巧

          (1)、多表关联查询的写法:

      var query = (from m in context.Dic_PropertyClassDef.Include("Dic_PropertyTypeDef")
                             .Include("Dic_PropertyTypeDef.Dic_PropertyValueDef")
                             .Include("Dic_PropertyTypeDef.Dic_PropertyOptValueDef")

                 这是一个多表关联查询的写法。可以不停的InCude下去。不过太复杂的业务建议还是用 sql或者存储过程更好些。

          (2)、EF4.1/4.3中的in语句:

                var query = Entities.Where(p => ids.Contains(p.ID));
                  ids 是本句中是你传的id列,可以是数组,泛型(List<int>)

          (3)、EF中的分页

                 query.OrderByDescending(r => r.PropID).Skip(startRowIndex).Take(maximumRows).ToList();

                 startRowIndex是起始索引,maximumRows要查询的条数。比哪startRowIndex=0,maximumRows=10 ,就是要从0开始查找10条记录。

                 EF的分布必需要与OrderByDescending一起使用。

原文地址:https://www.cnblogs.com/luckylei66/p/2594326.html