开发者

Why won't the Take() work in this Linq to Sql query

I want to take the first 10 records, but it is ignoring the .take(10) and returning all of the rows.

[WebMethod]
    public string getTopTenFeatured(int tab)
    {
        using (MyDataContext db = new MyDataContext())开发者_运维问答
        {

           IQueryable q = tab == 0? q = db.Items.Where(x => x.isFeatured == true)
               .OrderBy(x => x.title).Select(x=> x.title).Take(10): 
               q = db.Authors.Where(x=> x.isFeatured == true)
               .OrderBy(x => x.text).Select(x => x.text).Take(10);

            StringBuilder sb = new StringBuilder();

            sb.Append("<ul>");
            int i = 0;
            foreach (var n in q)
            {
                i++;
                sb.AppendFormat("<li>{0}. {1}</li>", i, n);
            }
            sb.Append("</ul>");

            return sb.ToString();    
        }
    }

Edit: Nothing wrong here, Projects as opposed to web sites appparently have to be rebuilded manually. Hitting refresh in the browser does not do this automatically like a web site type project.


I think you have too many q='s in there. Try this

   IQueryable q = (tab == 0) ?     

      db.Items
         .Where(x => x.isFeatured == true)    
         .OrderBy(x => x.title)
         .Select(x=> x.title).Take(10)  

   :  db.Authors.Where(x=> x.isFeatured == true)
          .OrderBy(x => x.text)
          .Select(x => x.text).Take(10);

Also, if you don't mind some constructive criticism, I don't think your web method should conflate these two return results. Perhaps have a separate web method for each -- more strongly typed. The checking of the tab should be done on the client side and the appropriate web method should be called. Just a thought. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜