Linq-To-SQL在空表中选择,更新和提交结果 [英] Linq-To-SQL select, update and commit results in empty Table

查看:64
本文介绍了Linq-To-SQL在空表中选择,更新和提交结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出:

using (DataContext ctx = new DataContext(props.ConnectionString))
{
    IQueryable<Email> emails = null;
    try
    {
       emails = ctx.Emails.Where(e => !(e.IsLocked || e.IsSent));
       foreach (var e in emails)
       {
           e.IsLocked = true;
       }
       ctx.SubmitChanges();
    }
}

// do something with emails here

为什么在SubmitChanges()之后电子邮件为空?有什么方法可以避免IsLocked设置为true后清空表?

Why is emails empty after SubmitChanges()? Is there any way to avoid emptying the Table after IsLocked is set to true?

推荐答案

ctx.Emails可能不为空.每次调用集合emails时都会对其进行评估.

The table ctx.Emails is probably not empty. The collection emails is evaluated every time you call it.

如果要保留首次通话时返回的电子邮件,可以执行以下操作:

You could do this if you want to keep the emails return on the initial call:

emails = ctx.Emails.Where(e => !(e.IsLocked || e.IsSent)).ToList().AsQueryable();

这篇关于Linq-To-SQL在空表中选择,更新和提交结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆