-1

我不知道如何将其转换为 Linq。任何帮助表示赞赏!

SELECT distinct top 5 searchterm, created
FROM
(   SELECT searchterm, created ,ROW_NUMBER() 
    over(partition by searchterm order by created) Ordresults, createdby
    FROM searchhistory 
) A
WHERE A.Ordresults = 1 and A.CreatedBy = 'USERNAME'
ORDER BY A.Created DESC
4

2 回答 2

1

像这样的东西应该工作:

查询语法:
Take不支持,所以必须作为扩展方法调用:

var query = (from term in EM.SearchHistory
            group term by term.searchterm into g
            let item = new 
            { 
                searchterm = g.Key, 
                first = g.OrderBy(t => t.created).First(),
            }
            where item.first.createdby == "USERNAME"
            orderby item.first.created
            select new
            {
                item.searchterm,
                item.first.created,
            })
            .Take(5)
;

方法语法:

var query = EM.SearchHistory
    .GroupBy(term => term.searchterm)
    .Select(g => new 
    {
        searchterm = g.Key, 
        first = g.OrderBy(t => t.created).First(),
    })
    .Where(item => item.first.createdby == "USERNAME")
    .OrderBy(item => item.first.created)
    .Select(item => new
    {
        item.searchterm,
        item.first.created,
    })
    .Take(5)
;
于 2012-12-13T17:12:09.073 回答
0

最后一点代码,如果它可以帮助其他人......

        var query = (from term in EM.SearchHistories
                     group term by term.SearchTerm into g
                     let item = new
                     {
                         searchterm = g.Key,
                         first = g.OrderBy(t => t.Created).FirstOrDefault(),
                     }
                     where item.first.CreatedBy == "USERNAME"
                     orderby item.first.Created
                     select new
                     {
                         item.searchterm,
                         item.first.Created,
                     })
        .Take(5);
于 2012-12-14T09:13:10.213 回答