日期:2014-05-20  浏览次数:20954 次

这个Linq查询为什么返回重复的记录
IEnumerable<IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo>> group =
  typeof(String).GetMembers().
  GroupBy(member => member.MemberType);

  foreach(IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> g in group)
  foreach (System.Reflection.MemberInfo mi in g)
  Console.WriteLine(g.Key + ":" + mi.Name);

------解决方案--------------------
那是因为有重载
C# code

        [TestMethod]
        public void Test01()
        {
            IEnumerable<IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo>> group =
                                                        typeof(String).GetMembers().
                                                        GroupBy(member => member.MemberType);

            foreach (IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> g in group)
                foreach (System.Reflection.MemberInfo mi in g)
                    Console.WriteLine(g.Key + ":" + mi.Name);

            typeof(String).GetMembers()
                .Select(p => new { MemberTypesName = p.MemberType.ToString(), MemberInfo = p.Name })
                .Distinct()
                .GroupBy(p => p.MemberTypesName)
                .ToList()
                .ForEach(p => p.ToList().ForEach(p1 => Console.WriteLine(p1.MemberTypesName + ":" + p1.MemberInfo)));


        }