Linq更新单行查询?

时间:2014-07-03 16:00:35

标签: c# wpf linq

我试图在单行的列上执行简单的UPDATE。我收到Sequence contains no elements例外。

private void InsertUpdateFileLocation(string targetFileLocationAndFullName, 
    tbFileAttachment linkGuid)
{
    ISESEntities context = new ISESEntities();

    var query = (from f in context.tbFileAttachments
                 where f.CCCPGUID == linkGuid.CCCPGUID
                 select f).First();

    query.FileLocation = targetFileLocationAndFullName;

    context.SaveChanges();
}

1 个答案:

答案 0 :(得分:4)

在这种情况下,您没有与您要搜索的值匹配的记录。您可以使用FirstOrDefault然后测试null:

来捕获此信息
var query = (from f in context.tbFileAttachments
             where f.CCCPGUID == linkGuid.CCCPGUID
             select f).FirstOrDefault();

if(query==null) 
{
    //no match found!
}