Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c实体表ServiceContext.CreateQuery_C#_Entity Framework_Error Handling - Fatal编程技术网

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上是否存在内部异常?