我现在已经完全空白,但我很想知道我会怎么做:
我需要在下面插入linq数据,但我不知道如何从链接中获取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>";
}
干杯
答案 0 :(得分:1)
正如评论中所提到的,使用<asp:Repeater>
肯定比连接包含HTML的字符串更好。无论如何,使用LINQ你可以编写例如:
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
或类似的东西是要走的路。)
答案 1 :(得分:1)
不是在go中添加所有数据,而是需要逐个访问它,因为for循环会给你这样的:
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>");