使用join检索linq中的10条记录

时间:2013-05-08 11:17:57

标签: web-services linq-to-sql xsd

我在我的webservice中有一个函数来检索从数据库发布的所有内容,我的代码工作正常:

    [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;
    }

1 个答案:

答案 0 :(得分: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);