Reputation: 187
I've got a complete blank at the moment, but I am wonder how I would do this:
I need to insert linq data into the following, but im not sure how to get 2 sets of data from a for look in link.
<ul>
<li>database value one</li>
<li>database value two</li>
<ul>
Here's an example I've whipped up showing what I'm trying to do:
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>";
}
Cheers
Upvotes: 0
Views: 300
Reputation: 18430
Instead of adding all data in go you will need to access it one by one as for loop will give you like this:
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>");
Upvotes: 1
Reputation: 243096
As mentioned in the comments, using <asp:Repeater>
would definitely be better than concatenating strings containing HTML. Anyway, using LINQ you can write for example something like this:
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>";
However, this won't be very fast, so using StringBuilder
would be probably better option than concatenating strings. (But still, Repeater
or something similar is the way to go).
Upvotes: 1