Linq实体类的设计(解决了复合查询的问题,同时解决了LINQ上下文缓存问题)

文章中解决的问题:1 Linq to SQL不支持返回显示实体问题

2 解决了实体中导航属性为集合时类型转换错误的问题

实体的设计:

/// <summary>
/// Linq原-生ú态类à型í的派é生ú类à,用来′临ù时±保£存结á果集ˉ
/// </summary>
public class WebManageUsers_Ext : WebManageUsers { }

/// <summary>
/// 对Linq原-生ú态类à型í的扩展1
/// </summary>
public partial class WebManageUsers : EntityBase
{
    /// <summary>
    /// 实μ体集ˉ合:o导航属性为a集ˉ合时±,不能ü直±接ó写′为aList,它ü会á有D类à型í转a换错í误ó,必须是IEnumerable接ó口ú类à型í
    /// </summary>
    public IEnumerable<WebManageUser_WebManageRoles> WebManageUser_WebManageRoles_Extend { get; set; }
    /// <summary>
    /// 实μ体
    /// </summary>
    public WebDepartments WebDepartments_Extend { get; set; }
    /// <summary>
    /// 统3一主÷键ü
    /// </summary>
    public override object[] PrimaryKey
    {
        get { return new object[] { this.ManageUserID }; }
    }
    /// <summary>
    /// 建¨立¢类à型í的对象ó时±,自动ˉ执′行D的代ú码逻辑-
    /// </summary>
    partial void OnCreated()
    {

        base.IsRealDeleted = false;//假ù删除y
        base.Initialization();//基ù类à的某3些属性初始化ˉ
        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(base.PropertyChangedEvent);//初始实μ体时±,先è订阅列D修T改的事件t
    }
}
而在进行进行复合查询时,我们可以这样来写:
var linq = (from data in iWebManageUsersRepository.GetModel()
                       join data2 in iWebDepartmentsRepository.GetModel() on data.DepartmentID equals data2.DepartmentID
                       join data3 in iWebManageUser_WebManageRolesRepository.GetDetailModel() on data.ManageUserID equals data3.ManageUserID into list
                       select new WebManageUsers_Ext
                       {
                           ManageUserID = data.ManageUserID,
                           LoginName = data.LoginName,
                           Password = data.Password,
                           RealName = data.RealName,
                           Gender = data.Gender,
                           Phone = data.Phone,
                           Mobile = data.Mobile,
                           Email = data.Email,
                           QQ = data.QQ,
                           MSN = data.MSN,
                           Rtx = data.Rtx,
                           Birthday = data.Birthday,
                           Description = data.Description,
                           DepartmentID = data.DepartmentID,
                           CreateDate = data.CreateDate,
                           UpdateDate = data.UpdateDate,
                           Operator = data.Operator,
                           Status = data.Status,
                           WebDepartments_Extend = data2,
                           WebManageUser_WebManageRoles_Extend = list.Cast<WebManageUser_WebManageRoles>(),
                           AvatarUrl = data.AvatarUrl,


                       });
在实体查询时,显式的返回了结果集,这样做的好处就是可以去除LINQ所带来的缓存问题下面是将用户及角色信息输出:
linq.ToList().ForEach(i =>
           {
               if (i.WebManageUser_WebManageRoles_Extend != null && i.WebManageUser_WebManageRoles_Extend.Count() > 0)
                   i.WebManageUser_WebManageRoles_Extend.ToList().ForEach(j => Console.WriteLine(j.WebManageRoles_Extend.RoleName + j.WebManageUsers_Extend.RealName));
           });
看似简单的知识,事实上它困扰了我很长时间,可能也困扰了您很长时间吧,没关系,今天咱们终于把它解决了。
posted @ 2012-04-27 10:49  张占岭  阅读(1079)  评论(0编辑  收藏  举报