var query =
     (from obj in objarray
      select obj.subObj.ID
     ).Distinct();在这个LinQ中,query.ToArray()得到的是整形(也就是ID)的数组
我想最后得到subObj的数组。
应该怎么做呢?

解决方案 »

  1.   

    var query = 
        (from obj in objarray 
          select obj.subObj 
        ).Distinct(); 
      

  2.   


    这个语句中。Distinct是按照subObj.ID对比么?
      

  3.   

    看看这个:class Objs
    {
        public int Value { get; set; }
        public override string ToString()
        {
            return Value.ToString();
        }
    }class Comp : IEqualityComparer<Objs>
    {
        #region IEqualityComparer<Objs> Members    public bool Equals(Objs x, Objs y)
        {
            return x.Value.Equals(y.Value);
        }    public int GetHashCode(Objs obj)
        {
            return obj.Value.GetHashCode();
        }    #endregion
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Objs> objs = new List<Objs> 
            { 
                new Objs { Value = 10 }, 
                new Objs { Value = 20 }, 
                new Objs { Value = 30 }, 
                new Objs { Value = 10 }
            };        var query = (from obj in objs
                         select obj).Distinct(new Comp());
            foreach (var obj in query)
                Console.WriteLine(obj.Value);
        }
    }
      

  4.   

    var query = 
        (from obj in objarray 
          select obj.subObj 
        ).Distinct(obj=>obj.ID); 
      

  5.   

    要写 IEqualityComparer 的子类,这个很麻烦。
    有没有好点儿的方法!
      

  6.   

    有问题,是按subObj.ID进行比对。不是obj.ID. 
    另外,我在.net里这么写,好像提示语法错误!
      

  7.   

    这样呢?static void Main(string[] args)
    {
        List<Objs> objs = new List<Objs> 
        { 
            new Objs { Value = 10 }, 
            new Objs { Value = 20 }, 
            new Objs { Value = 30 }, 
            new Objs { Value = 10 }
        };    var query = (from obj in objs
                     group obj by new {obj.Value} into p
                     select p);    foreach (var obj in query)
            Console.WriteLine(obj.Key.Value);
    }
      

  8.   

    其实这时候你的obj.Key就是你的obj
      

  9.   

    转换成我的情况是:
       var query = (from obj in objarray 
                     group obj by new {obj.subObj} into p
                     select p);  foreach (var obj in query)
            Console.WriteLine(obj.Key.ID);这个能实现按obj.subObj.ID group by么?