public async Task <IEnumerable <ResourceState> > LoadStateAsync(string tenant, string[] resourceIds = null, CancellationToken ctk = default(CancellationToken)) { Ensure.String.HasLengthBetween(tenant, 1, 128); if (resourceIds != null) { foreach (var r in resourceIds) { Ensure.String.HasLengthBetween(r, 1, 300); } } ResourceState map(ResourceState r, EJ e, MMJ m) { if (e?.ExtensionsJson != null) { r.Extensions = JsonConvert.DeserializeObject(e.ExtensionsJson, _jsonSerializerSettings); } if (m?.ModifiedSourcesJson != null) { r.ModifiedSources = JsonConvert.DeserializeObject <Dictionary <string, LocalDateTime> >(m.ModifiedSourcesJson, _jsonSerializerSettings); } return(r); } using (var c = _connManager.Get(_config.DbConnectionString)) { if (resourceIds == null) { return(await c.QueryAsync <ResourceState, EJ, MMJ, ResourceState>(_queryState , map , param : new { tenant = tenant } , splitOn : "ExtensionsJson,ModifiedSourcesJson") ); } else if (resourceIds.Length == 0) { return(Enumerable.Empty <ResourceState>()); //Empty array should just return empty result } else if (resourceIds.Length < 2000) //limit is 2100 { return(await c.QueryAsync <ResourceState, EJ, MMJ, ResourceState>(_queryState + " and [ResourceId] in @resources" , map , param : new { tenant = tenant, resources = resourceIds } , splitOn : "ExtensionsJson,ModifiedSourcesJson") ); } else { return(await c.QueryAsync <ResourceState, EJ, MMJ, ResourceState>(_queryState + " and [ResourceId] in (SELECT [ResourceId] FROM @resources)" , map , param : new { tenant = tenant, resources = resourceIds.Select(x => new { ResourceId = x }).ToDataTableArk().AsTableValuedParameter("udt_ResourceIdList") } , splitOn : "ExtensionsJson,ModifiedSourcesJson") ); } } }