Linq Distinct 指定属性过滤

        //省份
            this.RepAddressList.DataSource = mlist.Distinct(new FastPropertyComparer<A_pro_platform>("Province")).ToList();
            this.RepAddressList.DataBind();
            //首字母
            this.RepInitialList.DataSource = mlist.Distinct(new FastPropertyComparer<A_pro_platform>("PlatformNameFirstInitial")).ToList();
            this.RepInitialList.DataBind();
            //时间
            this.RepDtList.DataSource = mlist.Distinct(new FastPropertyComparer<A_pro_platform>("GetOnlineTimeYear")).ToList();
            this.RepDtList.DataBind();

 

public class A_pro_platform
    {
        #region 基本属性
        /// <summary>
        /// 平台编号
        /// </summary>        
        public Guid PlatformID { get; set; }
        /// <summary>
        /// 平台名称
        /// </summary>        
        public string PlatformName { get; set; }
        /// <summary>
        /// 平台名称首字母
        /// </summary>
        public string PlatformNameInitial { get; set; }
        /// <summary>
        /// 公司名称
        /// </summary>        
        public string Company { get; set; }
        /// <summary>
        /// 网站
        /// </summary>        
        public string Website { get; set; }
        /// <summary>
        /// 省份
        /// </summary>
        public string Province { get; set; }
        /// <summary>
        /// 城市
        /// </summary>        
        public int City { get; set; }
        /// <summary>
        /// 平台logo
        /// </summary>        
        public string PlatformLogo { get; set; }
        /// <summary>
        /// 办公地址
        /// </summary>        
        public string OfficeAddress { get; set; }
        /// <summary>
        /// 创始人
        /// </summary>        
        public string Founder { get; set; }
        /// <summary>
        /// 上线时间
        /// </summary>        
        public DateTime OnlineTime { get; set; }
        /// <summary>
        /// 注册资本(万元)
        /// </summary>        
        public int RegCapital { get; set; }
        /// <summary>
        /// IPC备案
        /// </summary>        
        public string IPC { get; set; }
        /// <summary>
        /// 公司规模
        /// </summary>        
        public string Scale { get; set; }
        /// <summary>
        /// 在线客服
        /// </summary>        
        public string ServiceQQ { get; set; }
        /// <summary>
        /// 服务电话
        /// </summary>        
        public string ServicePhone { get; set; }
        /// <summary>
        /// 总成交量
        /// </summary>        
        public int TotalVolume { get; set; }
        /// <summary>
        /// 投资总人数
        /// </summary>        
        public int TotalInvestors { get; set; }
        /// <summary>
        /// 成交周期
        /// </summary>        
        public int DealTime { get; set; }
        /// <summary>
        /// 项目估值
        /// </summary>        
        public int valuation { get; set; }
        /// <summary>
        /// 成交数量
        /// </summary>        
        public int DealNum { get; set; }
        /// <summary>
        /// 平台简介
        /// </summary>        
        public string PlatformPDL { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>        
        public int CreatedUserID { get; set; }
        /// <summary>
        /// 添加时间
        /// </summary>        
        public DateTime CreateDate { get; set; }
        /// <summary>
        /// 最新更新人
        /// </summary>        
        public int UpdateUserID { get; set; }
        /// <summary>
        /// 修改时间
        /// </summary>        
        public DateTime UpdateDate { get; set; }
        #endregion

        /// <summary>
        /// 名字的第一个字的首字母
        /// </summary>
        public string PlatformNameFirstInitial
        {
            get
            {
                string result = "";
                if (PlatformNameInitial.Length > 0)
                    result = PlatformNameInitial.Substring(0, 1);
                return result;
            }
        }
        /// <summary>
        /// 名字的第一个字的首字母
        /// </summary>
        public string GetOnlineTimeYear
        {
            get
            {
                string result = "";
                if (OnlineTime != null)
                    result = OnlineTime.ToString("yyyy");
                return result;
            }
        }


        #region 拓展属性
        /// <summary>
        /// 省份名称
        /// </summary>
        public string ProName { get; set; }
        /// <summary>
        /// 城市名称
        /// </summary>
        public string CityName { get; set; }
        #endregion

    }
View Code
拓展方法(网上COPY的)
[Serializable]
    public class FastPropertyComparer<T> : IEqualityComparer<T>
    {
        private Func<T, Object> getPropertyValueFunc = null;
        /// <summary>
        /// 通过propertyName 获取PropertyInfo对象
        /// </summary>
        /// <param name="propertyName"></param>
        public FastPropertyComparer(string propertyName)
        {
            PropertyInfo _PropertyInfo = typeof(T).GetProperty(propertyName,
            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
            if (_PropertyInfo == null)
            {
                throw new ArgumentException(string.Format("{0} is not a property of type {1}.",
                    propertyName, typeof(T)));
            }
            ParameterExpression expPara = Expression.Parameter(typeof(T), "obj");
            MemberExpression me = Expression.Property(expPara, _PropertyInfo);
            getPropertyValueFunc = Expression.Lambda<Func<T, object>>(me, expPara).Compile();
        }
        #region IEqualityComparer<T> Members
        public bool Equals(T x, T y)
        {
            object xValue = getPropertyValueFunc(x);
            object yValue = getPropertyValueFunc(y);
            if (xValue == null)
                return yValue == null;
            return xValue.Equals(yValue);
        }
        public int GetHashCode(T obj)
        {
            object propertyValue = getPropertyValueFunc(obj);
            if (propertyValue == null)
                return 0;
            else
                return propertyValue.GetHashCode();
        }
        #endregion
    }
View Code

http://www.jb51.net/article/36773.htm

posted @ 2015-05-07 19:37  厸厸  阅读(366)  评论(0编辑  收藏  举报