AzureStorageTableExtensions.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. using TEAMModelOS.SDK.Models.Table;
  2. using Microsoft.Azure.Cosmos.Table;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Linq;
  10. using TEAMModelOS.SDK;
  11. using System.Reflection;
  12. namespace TEAMModelOS.SDK.DI
  13. {
  14. public static class AzureStorageTableExtensions
  15. {
  16. #region CloudTable 批次操作
  17. /// <summary>
  18. /// 批次新增資料至Table儲存區。
  19. /// </summary>
  20. /// <param name="entities">欲快取的集合</param>
  21. /// <returns></returns>
  22. public static async Task<TableBatchResult> BatchInsertAsync<T>(this CloudTable table, IEnumerable<T> entities) where T : ITableEntity, new()
  23. {
  24. TableBatchOperation batchOperation = new TableBatchOperation();
  25. foreach (var cache in entities)
  26. {
  27. batchOperation.Insert(cache);
  28. }
  29. return await table.ExecuteBatchAsync(batchOperation);
  30. }
  31. #endregion
  32. #region CloudTable Get
  33. /// <summary>
  34. /// (同步)取得 table 中指定 PartitionKey 的所有集合。
  35. /// </summary>
  36. /// <returns></returns>
  37. public static IEnumerable<T> Get<T>(this CloudTable table, string partitionKey) where T : ITableEntity, new()
  38. {
  39. TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
  40. return table.ExecuteQuery(query);
  41. }
  42. /// <summary>
  43. /// (同步)取得指定 PartitionKey與 RowKey 的數據,效能:點查詢,最佳。
  44. /// </summary>
  45. /// <typeparam name="T">T</typeparam>
  46. /// <param name="partitionKey">PartitionKey</param>
  47. /// <param name="rowKey">RowKey</param>
  48. /// <returns></returns>
  49. public static T Get<T>(this CloudTable table, string partitionKey, string rowKey) where T : ITableEntity, new()
  50. {
  51. TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
  52. var retrieveResult = table.Execute(retrieveOperation);
  53. if (retrieveResult.Result != null)
  54. {
  55. //DynamicTableEntityJsonSeria1lizer
  56. return (T)retrieveResult.Result;
  57. }
  58. else
  59. {
  60. return default;
  61. }
  62. }
  63. public static DynamicTableEntity Get(this CloudTable table, string partitionKey, string rowKey)
  64. {
  65. TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey);
  66. var retrieveResult = table.Execute(retrieveOperation);
  67. if (retrieveResult.Result != null)
  68. {
  69. return retrieveResult.Result as DynamicTableEntity;
  70. }
  71. else
  72. {
  73. return null;
  74. }
  75. }
  76. /// <summary>
  77. /// 取得指定 PartitionKey 的所有集合(分頁),效能:範圍查詢,次佳。
  78. /// </summary>
  79. /// <typeparam name="T">T</typeparam>
  80. /// <param name="partitionKey">PartitionKey</param>
  81. /// <param name="takeCount">指定每次返回數量</param>
  82. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  83. /// <param name="onProgress">要返回集合的委派</param>
  84. /// <param name="ct">CancellationToken</param>
  85. /// <returns></returns>
  86. public static async Task GetAsync<T>(this CloudTable table, string partitionKey, int? takeCount = null, List<string> specifyPropertys = null, Action<IList<T>> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
  87. {
  88. TableQuery<T> tableQuery = null;
  89. TableContinuationToken token = null;
  90. var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
  91. if (specifyPropertys != null)
  92. tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
  93. else
  94. tableQuery = new TableQuery<T>().Where(filter);
  95. if (takeCount != null) tableQuery.TakeCount = takeCount;
  96. do
  97. {
  98. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  99. token = queryResponse.ContinuationToken;
  100. onProgress?.Invoke(queryResponse.Results);
  101. } while (token != null && !ct.IsCancellationRequested);
  102. }
  103. /// <summary>
  104. /// 取得指定 PartitionKey 的所有集合,效能:範圍查詢,次佳。
  105. /// </summary>
  106. /// <typeparam name="T">T</typeparam>
  107. /// <param name="partitionKey">PartitionKey</param>
  108. /// <param name="takeCount">指定每次返回數量</param>
  109. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  110. /// <param name="onProgress">要返回集合的委派</param>
  111. /// <param name="ct">CancellationToken</param>
  112. /// <returns></returns>
  113. public static async Task<IEnumerable<T>> GetAsync<T>(this CloudTable table, string partitionKey, int? takeCount = null, List<string> specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
  114. {
  115. TableQuery<T> tableQuery = null;
  116. TableContinuationToken token = null;
  117. var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
  118. if (specifyPropertys != null)
  119. tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
  120. else
  121. tableQuery = new TableQuery<T>().Where(filter);
  122. if (takeCount != null) tableQuery.TakeCount = takeCount;
  123. var items = new List<T>();
  124. do
  125. {
  126. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  127. token = queryResponse.ContinuationToken;
  128. items.AddRange(queryResponse.Results);
  129. } while (token != null && !ct.IsCancellationRequested);
  130. return items;
  131. }
  132. /// <summary>
  133. /// 傳回指定partitionKey的Count數量
  134. /// </summary>
  135. /// <param name="partitionKey"></param>
  136. /// <returns></returns>
  137. public static int GetCount(this CloudTable table, string partitionKey)
  138. {
  139. TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)).Select(new List<string> { "PartitionKey" });
  140. var queryResponse = table.ExecuteQuery(query).ToList();
  141. return queryResponse.Count();
  142. }
  143. #endregion
  144. #region CloudTable Get All
  145. /// <summary>
  146. /// (同步)取得 table 的所有集合,效能:資料表掃描,不佳。
  147. /// </summary>
  148. /// <returns></returns>
  149. public static IEnumerable<T> GetAll<T>(this CloudTable table) where T : ITableEntity, new()
  150. {
  151. return table.ExecuteQuery(new TableQuery<T>());
  152. }
  153. /// <summary>
  154. /// 取得 table 的所有集合(分頁),效能:資料表掃描,不佳。
  155. /// </summary>
  156. /// <param name="takeCount">指定每次返回數量</param>
  157. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  158. /// <param name="onProgress">要返回集合的委派</param>
  159. /// <returns></returns>
  160. public static async Task GetAllAsync<T>(this CloudTable table, int? takeCount = null, List<string> specifyPropertys = null, Action<IList<T>> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
  161. {
  162. TableContinuationToken token = null;
  163. TableQuery<T> tableQuery = new TableQuery<T>();
  164. if (specifyPropertys != null)
  165. tableQuery = new TableQuery<T>().Select(specifyPropertys);
  166. else
  167. tableQuery = new TableQuery<T>();
  168. if (takeCount != null) tableQuery.TakeCount = takeCount;
  169. var items = new List<T>();
  170. do
  171. {
  172. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  173. token = queryResponse.ContinuationToken;
  174. items.AddRange(queryResponse.Results);
  175. onProgress?.Invoke(queryResponse.Results);
  176. } while (token != null && !ct.IsCancellationRequested);
  177. }
  178. /// <summary>
  179. /// 取得 table 的所有集合,效能:資料表掃描,不佳。
  180. /// </summary>
  181. /// <param name="takeCount">指定每次返回數量</param>
  182. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  183. /// <returns></returns>
  184. public static async Task<IEnumerable<T>> GetAllAsync<T>(this CloudTable table, int? takeCount = null, List<string> specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
  185. {
  186. TableContinuationToken token = null;
  187. TableQuery<T> tableQuery = new TableQuery<T>();
  188. if (takeCount != null) tableQuery.TakeCount = takeCount;
  189. if (specifyPropertys != null)
  190. tableQuery = new TableQuery<T>().Select(specifyPropertys);
  191. else
  192. tableQuery = new TableQuery<T>();
  193. var items = new List<T>();
  194. do
  195. {
  196. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  197. token = queryResponse.ContinuationToken;
  198. items.AddRange(queryResponse.Results);
  199. } while (token != null && !ct.IsCancellationRequested);
  200. return items;
  201. }
  202. #endregion
  203. public static async Task<T> Save<T>(this CloudTable table, TableEntity entity) where T : TableEntity, new()
  204. {
  205. TableResult result = await table.ExecuteAsync(TableOperation.Insert(entity));
  206. return result.Result as T;
  207. }
  208. public static async Task<T> SaveOrUpdate<T>(this CloudTable table, TableEntity entity) where T : TableEntity, new()
  209. {
  210. TableResult result = await table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
  211. return result.Result as T;
  212. }
  213. public static async Task<T> Update<T>(this CloudTable table, TableEntity entity) where T : TableEntity, new()
  214. {
  215. TableResult result = await table.ExecuteAsync(TableOperation.Replace(entity));
  216. return result.Result as T;
  217. }
  218. public static async Task<T> Delete<T>(this CloudTable table, TableEntity entity) where T : TableEntity, new()
  219. {
  220. TableResult result = await table.ExecuteAsync(TableOperation.Delete(entity));
  221. return result.Result as T;
  222. }
  223. public static async Task<List<T>> DeleteAll<T>(this CloudTable table, List<T> entitys) where T : TableEntity, new()
  224. {
  225. if (!entitys.IsNotEmpty())
  226. {
  227. return null;
  228. }
  229. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  230. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  231. {
  232. Dictionary<string, List<T>> dictInfo = new()
  233. {
  234. { group.Key, group.ToList() }
  235. };
  236. listInfo.Add(dictInfo);
  237. }
  238. foreach (Dictionary<string, List<T>> dict in listInfo)
  239. {
  240. IList<TableResult> result = null;
  241. foreach (string key in dict.Keys)
  242. {
  243. List<T> values = dict[key];
  244. int pageSize = 100;
  245. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  246. for (int i = 0; i < pages; i++)
  247. {
  248. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  249. TableBatchOperation batchOperation = new();
  250. for (int j = 0; j < lists.Count; j++)
  251. {
  252. batchOperation.Delete(lists[j]);
  253. }
  254. result = await table.ExecuteBatchAsync(batchOperation);
  255. }
  256. }
  257. }
  258. return entitys;
  259. }
  260. public static async Task<List<T>> SaveOrUpdateAll<T>(this CloudTable table, List<T> entitys) where T : TableEntity, new()
  261. {
  262. if (!entitys.IsNotEmpty()) return null;
  263. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  264. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  265. {
  266. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  267. {
  268. { group.Key, group.ToList() }
  269. };
  270. listInfo.Add(dictInfo);
  271. }
  272. foreach (Dictionary<string, List<T>> dict in listInfo)
  273. {
  274. IList<TableResult> result = null;
  275. foreach (string key in dict.Keys)
  276. {
  277. List<T> values = dict[key];
  278. int pageSize = 100;
  279. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  280. for (int i = 0; i < pages; i++)
  281. {
  282. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  283. TableBatchOperation batchOperation = new TableBatchOperation();
  284. for (int j = 0; j < lists.Count; j++)
  285. {
  286. batchOperation.InsertOrReplace(lists[j]);
  287. }
  288. result = await table.ExecuteBatchAsync(batchOperation);
  289. }
  290. }
  291. }
  292. return entitys;
  293. }
  294. public static async Task<List<T>> UpdateAll<T>(this CloudTable table, List<T> entitys) where T : TableEntity, new()
  295. {
  296. if (!entitys.IsNotEmpty()) return null;
  297. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  298. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  299. {
  300. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  301. {
  302. { group.Key, group.ToList() }
  303. };
  304. listInfo.Add(dictInfo);
  305. }
  306. foreach (Dictionary<string, List<T>> dict in listInfo)
  307. {
  308. IList<TableResult> result = null;
  309. foreach (string key in dict.Keys)
  310. {
  311. List<T> values = dict[key];
  312. int pageSize = 100;
  313. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  314. for (int i = 0; i < pages; i++)
  315. {
  316. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  317. TableBatchOperation batchOperation = new TableBatchOperation();
  318. for (int j = 0; j < lists.Count; j++)
  319. {
  320. batchOperation.Replace(lists[j]);
  321. }
  322. result = await table.ExecuteBatchAsync(batchOperation);
  323. }
  324. }
  325. }
  326. return entitys;
  327. }
  328. public static async Task<List<T>> SaveAll<T>(this CloudTable table, List<T> entitys) where T : TableEntity, new()
  329. {
  330. if (!entitys.IsNotEmpty()) return null;
  331. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  332. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  333. {
  334. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  335. {
  336. { group.Key, group.ToList() }
  337. };
  338. listInfo.Add(dictInfo);
  339. }
  340. foreach (Dictionary<string, List<T>> dict in listInfo)
  341. {
  342. IList<TableResult> result = null;
  343. foreach (string key in dict.Keys)
  344. {
  345. List<T> values = dict[key];
  346. int pageSize = 100;
  347. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  348. for (int i = 0; i < pages; i++)
  349. {
  350. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  351. TableBatchOperation batchOperation = new TableBatchOperation();
  352. for (int j = 0; j < lists.Count; j++)
  353. {
  354. batchOperation.Insert(lists[j]);
  355. }
  356. result = await table.ExecuteBatchAsync(batchOperation);
  357. }
  358. }
  359. }
  360. return entitys;
  361. }
  362. public static List<T> FindListByDictSync<T>(this CloudTable table, Dictionary<string, object> dict) where T : TableEntity, new()
  363. {
  364. var exQuery = new TableQuery<T>();
  365. StringBuilder builder = new();
  366. if (null != dict && dict.Count > 0)
  367. {
  368. var keys = dict.Keys;
  369. int index = 1;
  370. foreach (string key in keys)
  371. {
  372. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  373. {
  374. string typeStr = SwitchType<T>(dict[key], key);
  375. if (string.IsNullOrEmpty(typeStr))
  376. {
  377. continue;
  378. }
  379. if (index == 1)
  380. {
  381. //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  382. builder.Append(typeStr);
  383. }
  384. else
  385. {
  386. //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  387. builder.Append(" " + TableOperators.And + " " + typeStr);
  388. }
  389. index++;
  390. }
  391. else
  392. {
  393. throw new Exception("The parameter must have value!");
  394. }
  395. }
  396. exQuery.Where(builder.ToString());
  397. return QueryListSync<T>(exQuery, table);
  398. }
  399. else
  400. {
  401. return null;
  402. }
  403. }
  404. public static async Task<List<T>> FindListByDict<T>(this CloudTable table, Dictionary<string, object> dict) where T : TableEntity, new()
  405. {
  406. var exQuery = new TableQuery<T>();
  407. StringBuilder builder = new();
  408. if (null != dict && dict.Count > 0)
  409. {
  410. var keys = dict.Keys;
  411. int index = 1;
  412. foreach (string key in keys)
  413. {
  414. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  415. {
  416. string typeStr = SwitchType<T>(dict[key], key);
  417. if (string.IsNullOrEmpty(typeStr))
  418. {
  419. continue;
  420. }
  421. if (index == 1)
  422. {
  423. //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  424. builder.Append(typeStr);
  425. }
  426. else
  427. {
  428. //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  429. builder.Append(" " + TableOperators.And + " " + typeStr);
  430. }
  431. index++;
  432. }
  433. else
  434. {
  435. throw new Exception("The parameter must have value!");
  436. }
  437. }
  438. exQuery.Where(builder.ToString());
  439. return await QueryList<T>(exQuery, table);
  440. }
  441. else
  442. {
  443. return null;
  444. }
  445. }
  446. private static List<T> QueryListSync<T>(TableQuery<T> exQuery, CloudTable TableName) where T : TableEntity, new()
  447. {
  448. TableContinuationToken continuationToken = null;
  449. List<T> entitys = new();
  450. do
  451. {
  452. var result = TableName.ExecuteQuerySegmented(exQuery, continuationToken);
  453. if (result.Results.Count > 0)
  454. {
  455. entitys.AddRange(result.ToList());
  456. }
  457. continuationToken = result.ContinuationToken;
  458. } while (continuationToken != null);
  459. return entitys;
  460. }
  461. private static async Task<List<T>> QueryList<T>(TableQuery<T> exQuery, CloudTable TableName ) where T : TableEntity, new()
  462. {
  463. TableContinuationToken continuationToken = null;
  464. List<T> entitys = new();
  465. do
  466. {
  467. var result = await TableName.ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  468. if (result.Results.Count > 0)
  469. {
  470. entitys.AddRange(result.ToList());
  471. }
  472. continuationToken = result.ContinuationToken;
  473. } while (continuationToken != null);
  474. return entitys;
  475. }
  476. private static string SwitchType<T>(object obj, string key)
  477. {
  478. Type objType = typeof(T);
  479. PropertyInfo property = objType.GetProperty(key);
  480. //Type s = obj.GetType();
  481. //TypeCode typeCode = Type.GetTypeCode(s);
  482. if (property == null)
  483. {
  484. //return null;
  485. throw new Exception(objType.FullName + " PropertyInfo doesn't include this parameter :" + key);
  486. }
  487. TypeCode typeCode = Type.GetTypeCode(property.PropertyType);
  488. switch (typeCode)
  489. {
  490. case TypeCode.String: return TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, obj.ToString());
  491. case TypeCode.Int32: return TableQuery.GenerateFilterConditionForInt(key, QueryComparisons.Equal, int.Parse(obj.ToString()));
  492. case TypeCode.Double: return TableQuery.GenerateFilterConditionForDouble(key, QueryComparisons.Equal, (double)obj);
  493. case TypeCode.Byte: return TableQuery.GenerateFilterConditionForBinary(key, QueryComparisons.Equal, (byte[])obj);
  494. case TypeCode.Boolean: return TableQuery.GenerateFilterConditionForBool(key, QueryComparisons.Equal, (bool)obj);
  495. case TypeCode.DateTime: return TableQuery.GenerateFilterConditionForDate(key, QueryComparisons.Equal, (DateTimeOffset)obj);
  496. case TypeCode.Int64: return TableQuery.GenerateFilterConditionForLong(key, QueryComparisons.Equal, long.Parse(obj.ToString()));
  497. default: return null;
  498. }
  499. }
  500. /// <summary>
  501. /// 查询条件传string类型
  502. /// </summary>
  503. /// <typeparam name="T"></typeparam>
  504. /// <param name="azureStorage"></param>
  505. /// <param name="strWhere">查询条件| 例:$"RowKey {QueryComparisons.GreaterThanOrEqual} '{123456}' {TableOperators.And} RowKey {QueryComparisons.LessThanOrEqual} '{123456}'" </param>
  506. /// <returns></returns>
  507. public static async Task<List<T>> QueryWhereString<T>(this CloudTable table, string strWhere = null) where T : TableEntity, new()
  508. {
  509. var exQuery = new TableQuery<T>();
  510. exQuery.Where(strWhere);
  511. return await QueryList<T>(exQuery, table);
  512. }
  513. /// <summary>
  514. /// 删除单个记录
  515. /// </summary>
  516. /// <typeparam name="T"></typeparam>
  517. /// <param name="azureStorage"></param>
  518. /// <param name="partitionKey"></param>
  519. /// <param name="rowKey"></param>
  520. /// <returns></returns>
  521. public static async Task<TableResult> DeleteSingle<T>(this CloudTable table, string partitionKey, string rowKey) where T : ITableEntity, new()
  522. {
  523. // query all rows
  524. var queryOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
  525. var tableResult = await table.ExecuteAsync(queryOperation);
  526. if (tableResult.Result is T item)
  527. {
  528. var dels = await table.ExecuteAsync(TableOperation.Delete(item));
  529. return dels;
  530. }
  531. else return null;
  532. }
  533. /// <summary>
  534. /// 删除多个 带有后期优化该方法
  535. /// </summary>
  536. /// <typeparam name="T"></typeparam>
  537. /// <param name="azureStorage"></param>
  538. /// <param name="rowKey"></param>
  539. /// <returns></returns>
  540. public static async Task<List<string>> DeleteStringWhere<T>(this CloudTable table, string rowKey) where T : ITableEntity, new()
  541. {
  542. // query all rows
  543. var exQuery = new TableQuery<T>();
  544. exQuery.Where(rowKey);
  545. TableContinuationToken continuationToken = null;
  546. List<T> entitys = new();
  547. do
  548. {
  549. var result = await table.ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  550. if (result.Results.Count > 0)
  551. {
  552. entitys.AddRange(result.ToList());
  553. }
  554. continuationToken = result.ContinuationToken;
  555. } while (continuationToken != null);
  556. List<string> ster = new List<string>();
  557. foreach (var item in entitys)
  558. {
  559. var tableResult = await table.ExecuteAsync(TableOperation.Retrieve<T>(item.PartitionKey, item.RowKey));
  560. if (tableResult.Result is T items)
  561. {
  562. var deleteOperation = TableOperation.Delete(items);
  563. await table.ExecuteAsync(deleteOperation);
  564. ster.Add(items.RowKey);
  565. }
  566. }
  567. return ster;
  568. }
  569. }
  570. }