以下查询用于获取类别和每个类别的一个新闻。如何使用LINQ
编写此查询SELECT * FROM News n where n.NewsID IN
(SELECT TOP 1 NewsID FROM News v
WHERE v.CategoryID = n.CategoryID
ORDER BY CreatedOn DESC)
提前致谢。
答案 0 :(得分:5)
未经测试,但尝试这样的事情:
using (var db = new YourDataContext())
{
var results = from n in db.News
let v = db.News
where n.NewsId == v.Where(c=>c.CategoryId == n.CategoryId)
.OrderByDescending(o=>o.CreatedOn).First()
select n;
}
答案 1 :(得分:0)
var q = from n in dc.News
group n by n.CategoryId into g
let ti = g.OrderByDescending(x => x.CreatedOn).FirstOrDefault()
where ti != null
select ti;
答案 2 :(得分:0)
这是在VB中:
Using db = New YourDataContext()
Dim results = From n In db.News _
Let v = db.News _
Where n.NewsId = v.Where(Function(c) c.CategoryId = n.CategoryId).OrderByDescending(Function(o) o.CreatedOn).First() _
Select n
End Using
转换为:http://www.developerfusion.com/tools/convert/csharp-to-vb/