我在ASP.NET中使用LINQ并有一个这样的代码:
for (int i = 0; i <= 2; i++)
{
IPost post = _postRepository.GetById(1);
txtTest.Text = post.Title;
post.Title = "test" + i.ToString();
}
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,我都会跟踪post.Title,但它的值是先前的值而不是数据库中的值:
txtTest.Text:
i=0 => what is in database
i=1 => test0
i=2 => test1
Run Code Online (Sandbox Code Playgroud)
我想解释一下我的主要问题:我有一个帖子课:
public class Post
{
Long Id { get; set; }
long Author { get; set; }
string Title { get; set; }
string Excerpt { get; set; }
string Content { get; set; }
ICollection<ShareLink> ShareLinks
{
get
{
IShareLinkRepository _shareLinkRepository = null;
_shareLinkRepository = ObjectFactory.GetInstance<IShareLinkRepository>();
ICollection<ShareLink> shareLInks =_shareLinkRepository.Get();
foreach (IShareLink shareLink in shareLInks)
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string newUrl = context.Request.Url.Scheme +
"://" + context.Request.Url.Authority +
context.Request.ApplicationPath.TrimEnd('/') + "/posts.aspx?p="
+ this.Id;
url.Replace("{title}", this.Tilte).Replace("{url}", newUrl);
}
return shareLInks;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和ShareLink是:
public class ShareLink
{
long Id { get; set; }
string Url { get; set; }
string Name { get; set; }
string Image { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
ShareLink.url的值如下所示:http://digg.com/submit?phase = 2&url = {url}&title = {title}在帖子类的ShareLinks字段中,ShareLinks.url的所有值都是第一个发布ShareLinks值
更改IPost post = _postRepository.GetById(1)
至
IPost post = _postRepository.GetById(1)
Run Code Online (Sandbox Code Playgroud)
(根据以下评论添加)
IPost post = _postRepository.GetById(i+1);
Run Code Online (Sandbox Code Playgroud)