var query =
(from obj in objarray
select obj.subObj.ID
).Distinct();在这个LinQ中,query.ToArray()得到的是整形(也就是ID)的数组
我想最后得到subObj的数组。
应该怎么做呢?
(from obj in objarray
select obj.subObj.ID
).Distinct();在这个LinQ中,query.ToArray()得到的是整形(也就是ID)的数组
我想最后得到subObj的数组。
应该怎么做呢?
(from obj in objarray
select obj.subObj
).Distinct();
这个语句中。Distinct是按照subObj.ID对比么?
{
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);
}
}
(from obj in objarray
select obj.subObj
).Distinct(obj=>obj.ID);
有没有好点儿的方法!
另外,我在.net里这么写,好像提示语法错误!
{
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);
}
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么?