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

去除某字段相同的记录求linq写法
表A 

字段: id name classId
记录1: 1 张三 1
记录2: 2 李四 1
记录3: 3 王五 2
记录4: 4 马六 3

需要得到记录1,3,4 怎么写linq? classid相同的项只取id最小的记录

sql应该怎么写?linq 语句又应该怎么写. 谢谢.在线等.

------解决方案--------------------
SQL写法:
select id, name,classId from A 
where id in (select min(id) from A group by classId)
------解决方案--------------------
select a.* from Tb a where a.ID= (select top 1 ID from Tb where classId=a.classId) order by a.ID
select a.* from Tb a where a.ID in (select min(ID) from t where group by classId) order by a.ID
------解决方案--------------------
var result1 = from a in DataContext.A
group a by a.age into g
select new {
a.Key
id = g.Min(t => t.ID)
};
var result2 = from a in DataContext.A
join b in result1 
on a.id Equals b.id
select a;
------解决方案--------------------
可以参考这些linq例子:http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspxhttp://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx
------解决方案--------------------
或者从页面 http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx 进入,点击 Aggregate Operators下面的例子,例如 Min - Elements 之类的。