C# c实体表ServiceContext.CreateQuery
我有以下代码:C# c实体表ServiceContext.CreateQuery,c#,entity-framework,error-handling,C#,Entity Framework,Error Handling,我有以下代码: var existsQuery = from e in TableServiceContext.CreateQuery<entity>(tableName) where e.PartitionKey == entity.PartitionKey
var existsQuery = from e
in TableServiceContext.CreateQuery<entity>(tableName)
where
e.PartitionKey == entity.PartitionKey
&& e.RowKey == entity.RowKey
select e;
entity existingObject;
try
{
existingObject = existsQuery.First();
}
catch (Exception) { existingObject = null; }
我只是想问一下,如果select语句没有返回任何值,我是否可以不使用try-catch,因为如果没有try-catch,我会抛出一个异常
我正在进行更新或插入
已提供图像。请尝试以下操作
var source = TableServiceContext.CreateQuery<entity>(tableName).Where(e=>e.PartitionKey == entity.PartitionKey && e.RowKey == entity.RowKey);
if(source != null && source.Any())
{
var existingObject = source.FirstOrDeFault();
// do somthing with existingObject
}else
{
// existingObject is null
}
如果为条件返回0条记录,则First将失败;如果使用FirstOrDefault,则在大多数情况下将返回null对象
var existsQuery = from e
in TableServiceContext.CreateQuery<entity>(tableName)
where
e.PartitionKey == entity.PartitionKey
&& e.RowKey == entity.RowKey
select e;
entity existingObject = existsQuery.FirstOrDefault();
existsQuery是否返回了任何记录?也许换成FirstOrDefault?行吗?DataServiceQueryException上是否存在内部异常?