ef的多表联查

来源:互联网 发布:四个字的网络歌手名字 编辑:程序博客网 时间:2024/05/21 10:38

思路:用ef 进行关联一张表然后生成新的数据,然后在依次进行关联,整合查询自己想要的数据代码如下

1.引入命名空间

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using System.Xml;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;using Newtonsoft.Json;using System.Data.Entity.SqlServer;using System.Web.SessionState; // session

2.在一般处理程序中进行编写
 public void ProcessRequest(HttpContext context)        {            context.Response.Clear();            context.Response.Write(GetGroupData(context).ToString());            context.Response.End();        }
3.写逻辑
 string ControllerName = context.Request.Form["ControllerName"].ToString();            string ActionName = context.Request.Form["ActionName"].ToString();            #region 开始处理            if (ControllerName == "StackDetail")            {                #region 用户部分登陆部分                if (ActionName == "LoadPagin")                {                    return Recombination.RB_SinLogin(context);                }                               #endregion            }
4.返回
    var cbbs = Pcontext.P2Pcontact.Where(n => n.P2PcontactId == Id)                              .Join(Pcontext.ContType, m => m.P2PType, n => n.ContTypeId, (m, n) => new                        {                            P2PcontactId = m.P2PcontactId,                            UserCustomerId = m.UserCustomerId,                            CommentUrlId = m.CommentUrlId,                            TypeName = n.TypeName, //P2PType                            P2PDateContent = m.P2PDateContent,                            P2PDate = m.P2PDate,                            SignC = m.SignC,                            CRelationId = m.CRelationId,                            CRelationType = m.CRelationType                        }).Join(Pcontext.CommentUrl, m => m.CommentUrlId, n => n.CommentUrlId, (m, n) => new                        {                            P2PcontactId = m.P2PcontactId,                            UserCustomerId = m.UserCustomerId,                            CommentUrl1 = n.CommentUrl1,                            TypeName = m.TypeName, //P2PType                            P2PDateContent = m.P2PDateContent,                            P2PDate = m.P2PDate,                            SignC = m.SignC,                            CRelationId = m.CRelationId,                            CRelationType = m.CRelationType                        }).Join(Pcontext.UserCustomer, m => m.UserCustomerId, n => n.UserCustomerId, (m, n) => new                        {                            P2PcontactId = m.P2PcontactId,                            UserName = n.UserName,                            CommentUrl1 = m.CommentUrl1,                            TypeName = m.TypeName, //P2PType                            P2PDateContent = m.P2PDateContent,                            P2PDate = m.P2PDate,                            SignC = m.SignC,                            CRelationId = m.CRelationId,                            CRelationType = m.CRelationType                        })                        .ToList();                    return JsonConvert.SerializeObject(new                    {                        Count = 1,                        DBData = cbbs                    });

原创粉丝点击