C# 值始终与以前一样,而不是数据库中的值

C# 值始终与以前一样,而不是数据库中的值,c#,linq,C#,Linq,我在ASP.NET中使用LINQ,代码如下: for (int i = 0; i <= 2; i++) { IPost post = _postRepository.GetById(1); txtTest.Text = post.Title; post.Title = "test" + i.ToString(); } 我有一门博士后课程: public class Post { Long Id { get; set; } long Author

我在ASP.NET中使用LINQ,代码如下:

for (int i = 0; i <= 2; i++)
{
    IPost post = _postRepository.GetById(1);
    txtTest.Text = post.Title; 
    post.Title = "test" + i.ToString();
}
我有一门博士后课程:

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;
        }
    }
}
ShareLink.url
的值如下:
http://example.com/submit?phase=2&url={url}&title={title}

在post类的
ShareLinks
字段中,
ShareLinks.url
的所有值都是第一个post
ShareLinks
值。

更改:

IPost post = _postRepository.GetById(1)

(根据以下注释添加)

IPost newPost=new IPost();//或者,您已经设法获取现有对象
IEnumerable allPosts=_postRepository.All();
foreach(所有邮政中的var邮政)
{
post.Url=String.Format(“digg.com/submit?phase=2&Url={0}&title={1}”,newPost.Url,newPost.title);
post.Save();
}   
更改:

IPost post = _postRepository.GetById(1)

(根据以下注释添加)

IPost newPost=new IPost();//或者,您已经设法获取现有对象
IEnumerable allPosts=_postRepository.All();
foreach(所有邮政中的var邮政)
{
post.Url=String.Format(“digg.com/submit?phase=2&Url={0}&title={1}”,newPost.Url,newPost.title);
post.Save();
}   
在这行中

post.Title = "test"+i.ToString();
您可以更改存储库中文章的标题。因此,该值将在后续迭代中读取。请记住,变量
post
在存储库中不包含该值的副本;它是对存储库中当前值的引用

具体情况如下:

迭代0:

post = value 1 from repository (Title = "what is in database")
txtTest.Text = "what is in database"
post.Title = "test0" (changes the value in the repository!)
迭代1:

post = value 1 from repository (Title = "test0")
txtTest.Text = "test0"
post.Title = "test1" (changes the value in the repository!)
迭代2:

post = value 1 from repository (Title = "test1")
txtTest.Text = "test1"
post.Title = "test2" (changes the value in the repository!)
在这一行

post.Title = "test"+i.ToString();
您可以更改存储库中文章的标题。因此,该值将在后续迭代中读取。请记住,变量
post
在存储库中不包含该值的副本;它是对存储库中当前值的引用

具体情况如下:

迭代0:

post = value 1 from repository (Title = "what is in database")
txtTest.Text = "what is in database"
post.Title = "test0" (changes the value in the repository!)
迭代1:

post = value 1 from repository (Title = "test0")
txtTest.Text = "test0"
post.Title = "test1" (changes the value in the repository!)
迭代2:

post = value 1 from repository (Title = "test1")
txtTest.Text = "test1"
post.Title = "test2" (changes the value in the repository!)


那么,LINQ到底在哪里使用?这个代码对meSo没有任何意义,LINQ到底在哪里使用?这个代码对Mestil没有任何意义,没有解释为什么他得到的是“测试”而不是第一个值这不是问题。问题是这些更改将保留到下一个查询,然后您需要保存所做的任何更改。您的示例没有遍历每个项,只返回for循环每次迭代的第一个项。(我应该在示例中加上I+1(现在已修改))我有一个包含书签站点的表。使用url名称fild。fied中的值如下:digg.com/submit?phase=2&url={url}&title={title}。我想用我的帖子url和标题更改每个帖子的{url}和{title}。并在嵌套的ListView中显示它。但这个问题发生了。我不确定我是否仍然有这个权利,但这是否更符合您的思路,请参考上面编辑并添加的内容?请参考逻辑,因为我不知道您的存储库及其用法。Till没有解释为什么他得到的是“test”而不是第一个值。这不是问题。问题是这些更改将保留到下一个查询,然后您需要保存所做的任何更改。您的示例没有遍历每个项,只返回for循环每次迭代的第一个项。(我应该在示例中加上I+1(现在已修改))我有一个包含书签站点的表。使用url名称fild。fied中的值如下:digg.com/submit?phase=2&url={url}&title={title}。我想用我的帖子url和标题更改每个帖子的{url}和{title}。并在嵌套的ListView中显示它。但这个问题发生了。我不确定我是否仍然有这个权利,但这是否更符合您的思路,请参考上面编辑并添加的内容?请参考逻辑,因为我不知道您的存储库及其usage@user806160:不更改post.Title。@user806160:不更改post.Title.;-)如果你向我解释一下,为什么你要更改post.Title,我可能会建议一个不同的解决方案。@Tavakoli:那真的没有帮助。让我更直接地问:你到底为什么写这行:
post.Title=“test”+i.ToString()?@Heinzi:我有一个包含书签站点的表。使用url名称fild。fied中的值如下:{url}&title={title}。我想用我的帖子url和标题更改每个帖子的{url}和{title}。并在嵌套的ListView中显示它。但是这个问题发生了。@Tavakoli:好吧,这有点复杂了。我建议您编辑您的问题,并清楚地说明:(A)最初数据库中有什么,(b)您希望在网页上看到什么输出,(c)在显示网页时,您希望数据库中的值如何更改。@user806160:不更改post.Title。@user806160:不更改post.Title.@user806160:不更改post.Title.-)如果你向我解释一下,为什么你要更改post.Title,我可能会建议一个不同的解决方案。@Tavakoli:那真的没有帮助。让我更直接地问:你到底为什么写这行:
post.Title=“test”+i.ToString()?@Heinzi:我有一个包含书签站点的表。使用url名称fild。fied中的值如下:{url}&title={title}。我想用我的帖子url和标题更改每个帖子的{url}和{title}。并在嵌套的ListView中显示它。但是这个问题发生了。@Tavakoli:好吧,这有点复杂了。我建议您编辑您的问题,并清楚地说明:(A)最初数据库中有什么,(b)您希望在网页上看到什么输出,(c)在显示网页时,您希望数据库中的值如何更改。