[英]Azure table storage: How to get a single entity by a partition key only?
I am trying to get a single entity provided only with a partition key. 我试图获得仅提供有分区键的单个实体。 My current code is returning me a List from IEnumerable, but it doesn't seem very efficient to have a list when in fact it is only a single entity, and I have to do a foreach through it.
我当前的代码正在从IEnumerable返回一个列表,但实际上它只是一个实体,拥有一个列表似乎并不是很有效,并且我必须遍历它。 Is there a way to achieve with without a list/IEnumerable?
没有列表/ IEnumerable,有没有办法实现?
var entities = currentTable.ExecuteQuery(new TableQuery<ItemEntity>()).Where(e => e.PartitionKey.Equals(itemEntity.xId)).ToList();
If you want to get a specified entity, you should provide Partition key and row key together. 如果要获取指定的实体,则应一起提供分区键和行键。
But if you want to get a random entity, you should use the methods provided by GvS. 但是,如果要获取随机实体,则应使用GvS提供的方法。
The result of the query is always an IEnumerable. 查询的结果始终是IEnumerable。
If you expect only a single result you can use: 如果只期望一个结果,则可以使用:
var result = currentTable.ExecuteQuery(new TableQuery<ItemEntity>()).Where(e => e.PartitionKey.Equals(itemEntity.xId));
// First element from the result. Will fail when there is no element
var entity1 = result.First();
// First element from the result. Will give null when no element is available.
var entity2 = result.FirstOrDefault();
// First element from the result. Will fail when there is no element, or more as one element
var entity3 = result.Single();
// First element from the result. Will give null when one element is available, or fail (Exeption) when more as one.
var entity4 = result.SingleOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.