总代码如下:
public ActionResult GetNoticeBC()
{
GDCPSIMEntities dc = new GDCPSIMEntities();
dc.Configuration.ProxyCreationEnabled = false;
int uid = (int)Session["UId"];
//关注的人的微博
var list1 = from u in dc.Users
join n in dc.Notice
on u.UserId equals n.Id
join b in dc.BroadCast
on n.To_Id equals b.UserId
join u2 in dc.Users
on n.To_Id equals u2.UserId
where u.UserId == uid
orderby b.BC_Time descending
select new
{
b.UserId,
b.BCId,
b.BC_Time,
b.SayText,
b.SayPic,
BCer_Pic = u2.MyPic,
BCer_Name = u2.UserNickname
};
//关注的人转发的微博
var list_notice_trans = from u in dc.Users
join n in dc.Notice
on u.UserId equals n.Id
join t in dc.Transport
on n.To_Id equals t.Id
join b in dc.BroadCast
on t.To_BCId equals b.BCId
join u1 in dc.Users
on t.Id equals u1.UserId
join u2 in dc.Users
on t.Id equals u2.UserId
where u.UserId == uid
orderby t.Trans_Time descending
select new
{
TranserName = u1.UserNickname,
TranserPic = u1.MyPic,
TransTime = t.Trans_Time,
TransSayText = t.Say_Text,
TransSayPic = t.Say_Pic,
BeTpedName = u2.UserNickname,
BeTpedPic = u2.MyPic,
BeTpedTime = b.BC_Time,
BeTpedSayText = b.SayText,
BeTpedSayPic = b.SayPic
};
//我的微博
var list2 = from b in dc.BroadCast
join u in dc.Users
on b.UserId equals u.UserId
where u.UserId == uid
orderby b.BC_Time descending
select new
{
b.UserId,
b.BCId,
b.BC_Time,
b.SayText,
b.SayPic,
BCer_Pic = u.MyPic,
BCer_Name = u.UserNickname,
};
//我转发的微博*********************************
var list_tran = from b in dc.BroadCast
join t in dc.Transport
on b.BCId equals t.To_BCId
join u1 in dc.Users
on t.Id equals u1.UserId
join u2 in dc.Users
on t.To_Id equals u2.UserId
where u1.UserId == uid
orderby t.Trans_Time descending
select new
{
TranserName = u1.UserNickname,
TranserPic = u1.MyPic,
TransTime = t.Trans_Time,
TransSayText = t.Say_Text,
TransSayPic = t.Say_Pic,
BeTpedName = u2.UserNickname,
BeTpedPic = u2.MyPic,
BeTpedTime = b.BC_Time,
BeTpedSayText = b.SayText,
BeTpedSayPic = b.SayPic
};
//集合
ArrayList list3 = new ArrayList();
list3.Add(list1);
list3.Add(list2);
list3.Add(list_tran);
list3.Add(list_notice_trans);
return Success(list3);
}
以下是总代码解析:
获取关注的人的微博,代码实现如下:
from u in dc.Users
join n in dc.Notice
on u.UserId equals n.Id
join b in dc.BroadCast
on n.To_Id equals b.UserId
join u2 in dc.Users //**********暂且不看
on n.To_Id equals u2.UserId //**********暂且不看
where u.UserId == uid
要获得我关注的人的微博,思路如下:
n.Id 是关注者id,n.To_Id 是被关注者的id,只要把 n.To_Id(被关注者的id)和 BroadCast表(微博表)关联起来就行了,即是这条语句:
join b in dc.BroadCast
on n.To_Id equals b.UserId
前提是要将我的 id 和 n.Id 关联起来 ,即是这三条语句 :
from u in dc.Users
join n in dc.Notice
on u.UserId equals n.Id
where u.UserId == uid
连接示意图如下:
而以下语句是----获取 n.To_Id (被关注者) 的昵称和头像
join u2 in dc.Users
on n.To_Id equals u2.UserId
网页效果:
获取关注的人转发的微博
from u in dc.Users
join n in dc.Notice
on u.UserId equals n.Id
join t in dc.Transport
on n.To_Id equals t.Id
join b in dc.BroadCast
on t.To_BCId equals b.BCId
join u1 in dc.Users //*********暂且不看
on t.Id equals u1.UserId //*********暂且不看
join u2 in dc.Users //*********暂且不看
on t.To_Id equals u2.UserId //*********暂且不看
where u.UserId == uid
orderby t.Trans_Time descending
t.Id 是转发者的id, t.To_Id 是被转发者的id,t.To_BCId 是被转发的微博的id,而 b.BCId 是微博表的主键id 自动增长。
网页效果:
获取我的微博:
from b in dc.BroadCast
join u in dc.Users
on b.UserId equals u.UserId
where u.UserId == uid
orderby b.BC_Time descending
获取我转发的微博:
from b in dc.BroadCast
join t in dc.Transport
on b.BCId equals t.To_BCId
join u1 in dc.Users
on t.Id equals u1.UserId
join u2 in dc.Users
on t.To_Id equals u2.UserId
where u1.UserId == uid
orderby t.Trans_Time descending
构造集合,存放4个结果集:
ArrayList list3 = new ArrayList();
list3.Add(list1.Distinct().OrderByDescending(b => b.BC_Time));
list3.Add(list2);
list3.Add(list_tran);
list3.Add(list_notice_trans);
return Success(list3);