English 中文(简体)
林克通过数据处理
原标题:Linq Looping through Data

现在我只剩下一个完整的空白,但我很想知道我如何这样做:

我需要将林克数据插入如下内容,但并不肯定如何从线上获取2套数据。

<ul>
<li>database value one</li>
<li>database value two</li>
<ul>

我举了一个例子,说明我试图做些什么:

foreach (blog_post h in recentBlogData)
        {

            recentString = recentString + "<ul>";

            DateTime blogDate = DateTime.Parse(h.blogPublishDate.ToString());

            recentString = recentString +
                "<li>" +
                    "<div class="date"><span>" + blogDate.ToString("%d") + "</span>" + blogDate.ToString("MMM").ToLower() + "</div>" +
                    "<strong><a href="" + s.getURL("SITE") + "/news/" + h.blogPostId + "/" + g.clean(h.blogPostTitle).ToLower() + "">" + h.blogPostTitle + "</a></strong>" +
                    "<div class="cl"></div>" +
                    "<p>" + h.blogReadMore + "<br />" +
                    "</p>" +
                "</li>";

            //this needs to be the next record.

            recentString = recentString +
                "<li>" +
                    "<div class="date"><span>" + blogDate.ToString("%d") + "</span>" + blogDate.ToString("MMM").ToLower() + "</div>" +
                    "<strong><a href="" + s.getURL("SITE") + "/news/" + h.blogPostId + "/" + g.clean(h.blogPostTitle).ToLower() + "">" + h.blogPostTitle + "</a></strong>" +
                    "<div class="cl"></div>" +
                    "<p>" + h.blogReadMore + "<br />" +
                    "</p>" +
                "</li>";

            recentString = recentString + "</ul>";
        }

卡车

最佳回答

不要在座标上增加所有数据,而是需要一对一旁听,这样就能使你:

StringBuilder recentstring = new StringBuilder();
recentstring.Append("<ul>");
foreach (blog_post h in recentBlogData)
        {   
            DateTime blogDate = DateTime.Parse(h.blogPublishDate.ToString());

            recentstring.Append("<li>");
                            recentstring.Append("<div class="date"><span>");
                            recentstring.Append(blogDate.ToString("%d"));
                            recentstring.Append("</span>");
                            recentstring.Append(blogDate.ToString("MMM").ToLower());
                            recentstring.Append("</div>");
                            recentstring.Append("<strong><a href="");
                            recentstring.Append(s.getURL("SITE") + "/news/" + h.blogPostId + "/" + g.clean(h.blogPostTitle).ToLower() + "">" + h.blogPostTitle)
                            recentstring.Append("</a></strong>");
                            recentstring.Append("<div class="cl"></div>");
                            recentstring.Append("<p>" + h.blogReadMore + "<br />");
                            recentstring.Append("</p></li>");
        }
recentstring.Append("</ul>");
问题回答

如评论意见所述,使用<代码><asp:Repeater>肯定优于对含有超文本的扼杀。 不管怎样,用准则编号,你可以这样写:

var q = 
  from h in recentBlogData
  let blogDate = DateTime.Parse(h.blogPublishDate.ToString());
  select "<li>" +
           "<div class="date"><span>" + blogDate.ToString("%d") + "</span>" 
              + blogDate.ToString("MMM").ToLower() + "</div>" +
           "<strong><a href="" + s.getURL("SITE") + "/news/" + h.blogPostId
              + "/" + g.clean(h.blogPostTitle).ToLower() + "">" 
              + h.blogPostTitle + "</a></strong>" +
           "<div class="cl"></div>" +
           "<p>" + h.blogReadMore + "<br />" +
           "</p>" + "</li>";
var result = "<ul>" + String.Concat(q) + "</ul>";

然而,这只得很快,因此,使用<条码>StringBuilder可能比加固地势更可取。 (仍然有<代码>Repeater或类似之处是行走的道路)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签