zoukankan      html  css  js  c++  java
  • 关于Linq的.distinct()方法的运用(一个简单的例子)

    目的:在GroupDetail表里查询所有不重复的年级和科目,并保存在ArrayList里

    在数据访问层

     internal class GroupDetailInfoDal : DaoImplements<int, GroupDetail>
        {
            internal GroupDetailInfoDal()
                : base("userCenter")
            {
            }

        internal ArrayList GetGrade()
            {
                ArrayList arrayGrade = new ArrayList();
                var query = (from x in this.Table
                                where x.GradeID != 0
                               select x.GradeID).Distinct();
                if(query != null)

        {
                  var qq = query.ToList();
                  for (int i = 0; i < qq.Count; i++)
                  {
                      arrayGrade.Add(qq[i]);
                  }

        }
                return arrayGrade;
            }

            /// <summary>
            /// 查出所有科目编号
            /// </summary>
            /// <returns></returns>
            internal ArrayList GetSubject()
            {
                var query = (from n in this.Table
                             where n.SubjectID != 0
                             select n.SubjectID).Distinct();
               
                ArrayList arraySubject = new ArrayList();

         if (query != null)
                {
                  var qq = query.ToList();
                      for (int i = 0; i < qq.Count;i++ )
                      {
                            arraySubject.Add(qq[i]);
                      }
                }
                return arraySubject;
            }

        }

    接收:

        //得到导航所有内容
            void GetAllDetail()
            {
                //得到所有年级
                ArrayList arrayGrade = new ArrayList();   //年级编号
               
                arrayGrade = GroupDetailInfoBll.GetGrade();
                string Gname = "";
                for (int i = 0; i < arrayGrade.Count; i++)
                {
                    Gname = GradeBll.Get(Convert.ToByte(arrayGrade[i])).Name;
                    arrayGName.Add(Gname);
                }

                //得到所有科目
                ArrayList arraySuject = new ArrayList();  //科目编号
                arraySuject = GroupDetailInfoBll.GetSubject();
                string SName = "";
                for (int j = 0; j < arraySuject.Count; j++)
                {
                    SName = SubjectBll.Get(Convert.ToInt16(arraySuject[j])).Name;
                    arraySName.Add(SName);
                }
            }

    就OK了!

  • 相关阅读:
    kafka 消息推送报错"dial tcp: lookup iZ2ze2k3******7abjcsyZ on 219.141.140.10:53: no such host"
    MacBook安装Homebrew的国内安装脚本 告别龟速下载安装
    docker info命令的使用和返回结果描述
    Python requests.post方法中data与json参数区别
    redis master-slave python switch logic (升级版,序列七)
    ELKF日志收集系统
    MySQL 中的concat函数使用
    程序员的街舞“梦”
    第一份工作没有工资,信不信
    Java中的享元设计模式,涨姿势了!
  • 原文地址:https://www.cnblogs.com/zqn518/p/2606208.html
Copyright © 2011-2022 走看看