2

我的网络服务中有一个功能可以检索从数据库中发布的所有内容,并且我的代码工作正常:

    [WebMethod]
    public XmlDataDocument return_FanWall_Posts()
    {
        var reportXmlItems = from y in db.fanwalls
                             join x in db.users on y.user_id equals x.id
                             orderby y.id descending
                             select new XElement("All_Fanwall",
                                  new XElement("post_ID", y.id),
                                  new XElement("post_image", y.pic),
                                  new XElement("post", y.comment),
                                  new XElement("post_date", y.date)
                                  new XElement("User_Name", x.user_name),
                                  new XElement("User_image", x.pic_url)
                              );

        var reportXml = new XElement("All_Fanwall", reportXmlItems);
        XmlDataDocument m = new XmlDataDocument();
        m.LoadXml(reportXml.ToString());
        return m;
    }

现在我需要检索前 10 条记录,然后检索接下来的 10 条记录,而这段代码不起作用,结果是检索表中的所有记录:

    [WebMethod]
    public XmlDataDocument return_FanWall_Posts(int iBeginRowIndex, int iMaximumRows)
    {
        var reportXmlItems = from y in db.fanwalls
                             join x in db.users on y.user_id equals x.id
                             orderby y.id descending
                             select new XElement("All_Fanwall",
                                  new XElement("post_ID", y.id),
                                  new XElement("post_image", y.pic),
                                  new XElement("post", y.comment),
                                  new XElement("post_date", y.date)
                                  new XElement("User_Name", x.user_name),
                                  new XElement("User_image", x.pic_url)
                              );

        reportXmlItems.Skip(iBeginRowIndex).Take(iMaximumRows);
        var reportXml = new XElement("All_Fanwall", reportXmlItems);
        XmlDataDocument m = new XmlDataDocument();
        m.LoadXml(reportXml.ToString());
        return m;
    }
4

1 回答 1

2

将查询括起来并标记Skip()Take()调用它...

var reportXmlItems = (from y in db.fanwalls
                      join x in db.users on y.user_id equals x.id
                      orderby y.id descending
                      select new XElement("All_Fanwall",
                                 new XElement("post_ID", y.id),
                                 new XElement("post_image", y.pic),
                                 new XElement("post", y.comment),
                                 new XElement("post_date", y.date),
                                 new XElement("User_Name", x.user_name),
                                 new XElement("User_image", x.pic_url)
                             )
                      ).Skip(iBeginRowIndex).Take(iMaximumRows);
于 2013-05-08T11:29:21.913 回答