Loading

LINQ 中使用 Distinct 的 Compare过滤重复的字段

    public class ProcurementPhotoMenuCompare : IEqualityComparer<ProcurementPhotoMenuModel>
    {
        public bool Equals(ProcurementPhotoMenuModel x, ProcurementPhotoMenuModel y)
        {
            return (x.CategoryType == y.CategoryType);
        }

        public int GetHashCode(ProcurementPhotoMenuModel obj)
        {
            return obj.ToString().ToLower().GetHashCode();
        }
    }

使用;

return m.Select(t =>
                                    new ProcurementPhotoMenuModel
                                        {
                                            CategoryType = t.Identifier ?? "",
                                            MenuTitle = t.Title ?? "",
                                            PhotoMenuType = t.Attribute01 ?? ""
                                        }).Distinct(new ProcurementPhotoMenuCompare()).ToList();

 

posted @ 2012-12-17 16:35  Dhoopu  阅读(2453)  评论(0编辑  收藏  举报