从两个集合里排除重复的写法(适用:DB表和字段都很多,表间有关联的情况)

获取其中一张表bulletinred为1的内容:

 1 public IList<BRShow> GetBulInfo()
 2         {
 3             var result = from a in ((Entities)this.DataContext).SPEPBULLETIN
 4                          join b in ((Entities)this.DataContext).USERS on a.USERID.ToUpper() equals b.ACCOUNT.ToUpper()
 5 
 6                          orderby a.POSTDATE descending
 7                          select new BRShow { ID = a.ID, TITLE = a.TITLE, NAME = b.NAME, POST_LEVEL = a.POST_LEVEL, POSTCONTENT = a.POSTCONTENT, POSTDATE = a.POSTDATE, bulletinread = 0 }; //查询出所有信息
 8 
 9             //20140402  By Xudaxia  遍历result,取得为1的条数
10             var result2 = (from r in result
11                            join b in ((Entities)this.DataContext).BULLETIN_RECORDS
12                            on r.ID equals b.ID
13                            select r).ToList();
14             foreach (var r in result2) { r.bulletinread = 1; };
15             return result2;
16         }

获取表所有信息:

 1 public IList<BRShow> GetBulInfo1()
 2         {
 3             var spepbulletins = ((Entities)this.DataContext).SPEPBULLETIN.ToList();
 4             var users = ((Entities)this.DataContext).USERS.ToList();
 5             var result = from a in spepbulletins
 6                          join b in users on a.USERID.ToUpper() equals b.ACCOUNT.ToUpper()
 7                          orderby a.POSTDATE descending
 8                          select new BRShow { ID = a.ID, TITLE = a.TITLE, NAME = b.NAME, POST_LEVEL = a.POST_LEVEL, POSTCONTENT = a.POSTCONTENT, POSTDATE = a.POSTDATE}; //查处所有信息(含未读和已读)
 9             return result.ToList();
10         }

在页面上调用,转为LIST后在所有信息中遍历取得ID,判断A表和B表的ID是否一致,如果一直则将bulletinred赋值为1

1 List<BRShow> list = bulRep.GetBulInfo().ToList();//查处所有已读信息
2         List<BRShow> list1 = bulRep.GetBulInfo1().ToList();
3         foreach (BRShow b in list)
4         {
5             if (list1.Where(l => l.ID == b.ID).First() != null)
6                 list1.Where(l => l.ID == b.ID).First().bulletinread = 1;
7         }

最后取得所有相关内容

List<BRShow> tt = list1;

----附:欢迎大家提出更简便、性能更好的方法~

posted @ 2014-04-04 18:00  许大虾  阅读(994)  评论(0编辑  收藏  举报