帮忙改一条SQL语句啊

boozai 2010-10-13 02:26:11
select * from tb where id in(
select max(id)
from tb
group by 类别)
怎么变成linq
...全文
129 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
huminghua 2010-10-14
  • 打赏
  • 举报
回复
from s in tb where (from t in tb group t by t.类别 into a select(a=>a.id).Max()).Contains(s.id) select s;
光脚丫思考 2010-10-13
  • 打赏
  • 举报
回复
PxxxP代码给了我一个很大的启发,不过也有几个错误:
首先,from子句后面没有跟in子句,另外,当from子句中指定的范围变量在分组的时候放入到由into子句指定的表示分组的范围变量中之后

,就无法在后面的语句中使用。第三个错误就是where子句的逻辑运算符应该是两个==,而不是一个=。
我想着应该是没有在VS中手写所造成的吧!^_^

基于PxxxP的这一想法,我使用了下面的方式实现的,经过测试了,没有什么问题。
不过,我使用的是Northwind数据库,是对产品进行分组,并且获取每个分组中最高价的产品。
别忘记调用First()扩展方法了,否则会出错的。
另外,为了方便查看,我将查询的结果按照分类编号由小到大进行排序了,否则的话,看起来有些凌乱。

NorthwindDataContext db = new NorthwindDataContext();
var Products =
from ProductObject in db.Products
orderby ProductObject.CategoryID
where ProductObject.UnitPrice ==
(from P in db.Products
where P.CategoryID == ProductObject.CategoryID
group P by P.CategoryID into ProductGroup
select ProductGroup.Max(P2 => P2.UnitPrice)).First()
select ProductObject;

foreach (var ProductObject in Products)
{
Console.WriteLine("---------------------");
Console.WriteLine("Category ID : {0}",
ProductObject.CategoryID);
Console.WriteLine("Max Unit Price : {0}",
ProductObject.UnitPrice);

Thread.Sleep(1000);
}

下面的代码则是使用传统的做法,具体的说,是首先将产品按照分类进行分组,然后查找出每个分组中的最高价的产品。
NorthwindDataContext db = new NorthwindDataContext();
var Products =
from ProductObject in db.Products
group ProductObject by ProductObject.CategoryID into ProductGroup
from P in ProductGroup
where P.UnitPrice == ProductGroup.Max(P2 => P2.UnitPrice)
select P;

foreach (var ProductObject in Products)
{
Console.WriteLine("---------------------");
Console.WriteLine("Category ID : {0}", ProductObject.CategoryID);
Console.WriteLine("Max Unit Price : {0}", ProductObject.UnitPrice);

Thread.Sleep(1000);
}

再次对PxxxP表示感谢,它的代码虽然有误,但是想法不错。说不定哪天我就用上了!
q107770540 2010-10-13
  • 打赏
  • 举报
回复
var qurey= from s in tb
let maxid= from t in tb
group t by t.类别 into m
select new {m.Max(t=>t.id)}
where maxid.Contains(s.id)
select s;

那试试这个
PxxxP 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 pxxxp 的回复:]

C# code

//没测试,手写的
var Query=from tb t
where t.id==(from tb t1 group t1 by t1.类别 into g where t.类别=t1.类别 select max(i=>g.id))
select new{max(id=>g.id)}
[/Quote]
手误

var Query=from tb t
where t.id==(from tb t1 group t1 by t1.类别 into g where t.类别=t1.类别 select max(i=>g.id))
select t
PxxxP 2010-10-13
  • 打赏
  • 举报
回复

//没测试,手写的
var Query=from tb t
where t.id==(from tb t1 group t1 by t1.类别 into g where t.类别=t1.类别 select max(i=>g.id))
select new{max(id=>g.id)}
PxxxP 2010-10-13
  • 打赏
  • 举报
回复
流氓,你搞错了,人家是对类别进行分组,然后在取每个类别下最大的那个!
q107770540 2010-10-13
  • 打赏
  • 举报
回复

var query=( from t in tb
orderby t.id descending
select t).FirstOrDefault();

这样也可以
q107770540 2010-10-13
  • 打赏
  • 举报
回复
sorry ..更正一下:

var query= tb.OrderByDescending(t=>t.id).First();
q107770540 2010-10-13
  • 打赏
  • 举报
回复
var query= tb.Max(t=>t.id);

你的group by 类别放在括号内没有任何意义

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧