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

怎样以下SQL语句转为LINQ查询?
自己转换了一个,但是得不到期望的结果...
SQL语句:
SQL code
select top 5    authorName,count(*)
from dbo.authors
group by authorName
having authorName like '赵%'
order by   count(*) desc 


自己转换的LINQ查询: ?
C# code

  V1DataContext DB = new V1DataContext();
                    var queryName = (from A in DB.authors
                                     group A by new
                                     {
                                         authorName = A.authorName
                                     } into grA
                                     select new
                                     {
                                         authorName = grA.Key.authorName,
                                         Quantity = grA.Count()
                                     } into orderbygrA
                                     where  orderbygrA.authorName.StartsWith("张")
                                     orderby orderbygrA.Quantity descending
                                     select orderbygrA.authorName).Distinct().Take(7);



------解决方案--------------------
给你推荐个Linq工具 LinqPad ,可以把linq查询的语句转成sql
------解决方案--------------------
C# code

var query=(from A in DB.authors.Where(b=>b.authorName.StartsWith("张"))
          group A by A.authorName into g
          let c=g.Count()
          orderby c desending
          select new {authorName=g.Key,cnt=c}).Take(5);