list合并与连接

问题来源:

     一个数据库表中的id来源于另外两个表,不确定是哪个表,但是可以确定的是两个表都可能,我要通过这个id去另外两个表匹配,查询出具体的Name.如果是直接的外键使用EF框架直接用导航属性就可以,但是现在不符合。


     现在看一下我具体的表,可能更详细一些:

list合并与连接_第1张图片


list合并与连接_第2张图片


list合并与连接_第3张图片

现在我第一张表的ReceiveUnit来源于表A的CityID和表B的UnitID,表A和表B的字段个数和具体的含义是一样的


实现思路:


       把表A和表B合并到一起,这样合并到一起的id列和ReceiveUnit字段的id就会对应起来,连接这两列选取我需要的内容。


代码部分:


       将表A和表B的信息合并到一起:

     

<span style="font-family:KaiTi_GB2312;font-size:18px;">//定义个泛型集合变量
            //1.查询出所有的市直单位的信息
            List<UnitBasicInfoViewModel> vunitbasic = new List<UnitBasicInfoViewModel>();
            UnitBasicInfoBll unitbasicbll = new UnitBasicInfoBll();
            vunitbasic = unitbasicbll.queryAllUnit();
            
            //2.查询出所有的县市区的信息
            List<CityBasicInfoViewModel> vcitybasic = new List<CityBasicInfoViewModel>();
            CityBasicInfoBll citybasicbll = new CityBasicInfoBll();
            vcitybasic = citybasicbll.queryAllCity();
            //3.将县市区和市直单位两个信息合并到一起
            List<UnitBasicInfoViewModel> componet = ComponetAB(vunitbasic, vcitybasic);</span>


其中ComponetAB是一个私有的方法,把citybasicinfoviewmodel合并到UnitBasicInfoViewModel里面


<span style="font-family:KaiTi_GB2312;font-size:18px;">#region 将县市区和市直单位的表合并到一起-韦文文2016年3月8日22:06:49
        private List<UnitBasicInfoViewModel> ComponetAB(List<UnitBasicInfoViewModel> vunitbasic, List<CityBasicInfoViewModel> vcitybasic)
        {
            foreach (var item in vcitybasic)
            {
                UnitBasicInfoViewModel unitinfo = new UnitBasicInfoViewModel();
                unitinfo.DictionaryID = item.DictionaryID;
                unitinfo.Operator = item.Operator;
                unitinfo.UnitID = item.CityID;
                unitinfo.UnitName = item.CityName;
                vunitbasic.Add(unitinfo);
            }
            return vunitbasic;
        }
    #endregion</span>


将AssignmentViewModel和UnitBasicInfoViewModel对应的id进行连接返回


<span style="font-family:KaiTi_GB2312;font-size:18px;">List<AssignmentViewModel> enAssignmentlist = new List<AssignmentViewModel>();

            List<T_Assignment> AssignmentList = this.CurrentDal.LoadPageItems(pageSize, pageIndex, out total, o => o.IsEnable == 1,o=>o.PublishTime,true).ToList();
            //遍历查询所有
            foreach (var item in AssignmentList)
            {
                List<UnitBasicInfoViewModel> test = componet.Where(p => p.UnitID == item.ReceiveUnit).ToList();
                
                AssignmentViewModel assignment = new AssignmentViewModel()                
                {    
                AssignmentID = item.AssignmentID,
                AssignmentName=item.AssignmentName,
                PublishUnit=item.PublishUnit,
                PublishTime = (DateTime)item.PublishTime,
                Title=item.Title,
                Mainbody=item.Mainbody,
                AccessoryAddress=item.AccessoryAddress, 
                IsRead=item.IsRead.ToString(),
                IsEnable=(int)item.IsEnable,
                Operator=item.Operator,                
                ReceiveUnitName = test[0].UnitName
                
                };
                string timepublish=item.PublishTime.ToString();
                assignment.PublishTimeResult=timepublish.Substring(0,9);
                //将数字转换为我们需要的
                switch (assignment.IsRead)
                {
                    case "0": assignment.CIsRead = "未读"; break;
                    case "1": assignment.CIsRead = "已读"; break;
                }
            enAssignmentlist.Add(assignment);
            }
            
            return enAssignmentlist;</span>


小结:


     什么知识都是需要积累的,这只是当初的一个实现思路,也是需要锻炼我们思维。有其他的方法也请给出建议





你可能感兴趣的:(list合并与连接)