首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

去除某字段相同的记录求linq写法,该怎么解决

2011-12-28 
去除某字段相同的记录求linq写法表A字段:idnameclassId记录1:1张三1记录2:2李四1记录3:3王五2记录4:4马六3

去除某字段相同的记录求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 之类的。

热点排行