public void UpdateAirQualityForContactsBatch(int size) { using (var client = SitecoreXConnectClientConfiguration.GetClient()) { try { var goalId = Guid.Parse(RegisterGoalId); IAsyncQueryable <Contact> queryable = client.Contacts .Where(c => c.Interactions.Any(f => f.Events.OfType <Goal>().Any(a => a.DefinitionId == goalId))) .WithExpandOptions(new ContactExpandOptions() { Interactions = new RelatedInteractionsExpandOptions() { Limit = 10 } }); var enumerable = queryable.GetBatchEnumeratorSync(size); while (enumerable.MoveNext()) { foreach (var contact in enumerable.Current) { UpdateAirQualityForContact(contact); } } } catch (XdbExecutionException ex) { Log.Error($"Error updating air quality in batch", ex, this); } } }
public async Task ElementAtOrDefaultAsyncWithInt32SourceWithIndexNullSourceThrowsArgumentNullExceptionTest() { // Arrange // Arrange 'queryAdapter' parameter var queryAdapter = await GetQueryAdapterAsync(DisallowAll); // Arrange 'asyncSource' parameter IAsyncQueryable <int> asyncSource = null !; // Arrange 'index' parameter var index = 5; // Arrange 'cancellationToken' parameter var cancellationToken = CancellationToken.None; // Act // - // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { await AsyncQueryable.ElementAtOrDefaultAsync <int>(asyncSource, index, cancellationToken).ConfigureAwait(false); }); }
public AsyncQueryCleanerTest() { query = Enumerable.Empty <Dummy>() .ToAsyncEnumerable() .AsAsyncQueryable() .OrderBy(d => d.Id); }
/// <summary> /// Asynchronously iterates over the input sequence and performs the specified action on each element of the <see cref="IAsyncQueryable{T}"/>. /// Return <see langword="true"/> from <paramref name="action"/> to cause the loop to gracefully break; <see langword="false"/> to continue looping. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An <see cref="IAsyncQueryable{T}"/> containing items to operate on.</param> /// <param name="action">The action to perform each element. Return <see langword="true"/> to cause the loop to gracefully break.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> public static async Task ForEachAsync <TSource>(this IAsyncQueryable <TSource> source, Func <TSource, bool> action, CancellationToken cancellationToken = default(CancellationToken)) { if (action == null) { throw new ArgumentNullException(nameof(action)); } while (await source.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { bool breakRequested = false; foreach (var item in source.CurrentPage) { cancellationToken.ThrowIfCancellationRequested(); breakRequested |= action(item); if (breakRequested) { break; } } if (breakRequested) { break; } } }
/// <summary> /// Create a dynamic where clause for a given property selector, comparison method and reference value. /// </summary> /// <typeparam name="T">The type of the query data.</typeparam> /// <param name="query">The query to filter.</param> /// <param name="selector">The property selector to parse.</param> /// <param name="comparer">The comparison method to use.</param> /// <param name="value">The reference value to compare with.</param> /// <param name="provider">The culture-specific formatting information.</param> /// <returns>The filtered query.</returns> public static IAsyncQueryable <T> Where <T>(this IAsyncQueryable <T> query, string selector, string comparer, string?value, IFormatProvider?provider = null) { if (query is null) { throw new ArgumentNullException(nameof(query)); } if (string.IsNullOrEmpty(selector)) { throw new ArgumentNullException(nameof(selector)); } if (string.IsNullOrEmpty(comparer)) { throw new ArgumentNullException(nameof(comparer)); } var target = Expression.Parameter(typeof(T)); var comparison = DynamicExpression.CreateComparison(target, selector, comparer, value, provider); return(query.Provider.CreateQuery <T>( CreateAsyncWhereClause(target, query.Expression, comparison))); }
public static Task <TSource> SingleOrDefaultAsync <TSource>(this IAsyncQueryable <TSource> source, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <TSource>( Expression.Call( GetMethodInfo <IQueryable <TSource>, TSource>(Queryable.SingleOrDefault, source), source.Expression), cancellationToken)); }
public static Task <long?> SumAsync(this IAsyncQueryable <long?> source, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <long?>( Expression.Call( GetMethodInfo <IQueryable <long?>, long?>(Queryable.Sum, source), source.Expression), cancellationToken)); }
public static Task <List <TSource> > ToListAsync <TSource>(this IAsyncQueryable <TSource> source, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <List <TSource> >( Expression.Call( GetMethodInfo <IEnumerable <TSource>, List <TSource> >(Enumerable.ToList, source), source.Expression), cancellationToken)); }
public static Task <double?> AverageAsync(this IAsyncQueryable <int?> source, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <double?>( Expression.Call( GetMethodInfo <IQueryable <int?>, double?>(Queryable.Average, source), source.Expression), cancellationToken)); }
public static Task <IQueryable <TSource> > SkipAsync <TSource>(this IAsyncQueryable <TSource> source, int count, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <IQueryable <TSource> >( Expression.Call( GetMethodInfo <IQueryable <TSource>, int, IQueryable <TSource> >(Queryable.Skip, source, count), source.Expression, Expression.Constant(count)), cancellationToken)); }
private static void AssertQuery(IAsyncQueryable actual) { _ = Assert.IsType <RewriteAsyncQueryable <Dummy> >(actual); var actualProvider = Assert.IsType <RewriteAsyncQueryProvider>(actual.Provider); _ = Assert.IsType <InjectableQueryRewriter>(actualProvider.Rewriter); _ = Assert.IsAssignableFrom <IAsyncQueryProvider>(actualProvider.Provider); }
public static Task <long?> SumAsync <TSource>(this IAsyncQueryable <TSource> source, Expression <Func <TSource, long?> > selector, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <long?>( Expression.Call( GetMethodInfo <IQueryable <TSource>, Expression <Func <TSource, long?> >, long?>(Queryable.Sum, source, selector), source.Expression, Expression.Quote(selector)), cancellationToken)); }
public static Task <TSource> SingleAsync <TSource>(this IAsyncQueryable <TSource> source, Expression <Func <TSource, bool> > predicate, CancellationToken cancellationToken = default(CancellationToken)) { return(((IAsyncQueryProvider)source.Provider).ExecuteAsync <TSource>( Expression.Call( GetMethodInfo <IQueryable <TSource>, Expression <Func <TSource, bool> >, TSource>(Queryable.Single, source, predicate), source.Expression, Expression.Quote(predicate)), cancellationToken)); }
public static int GetPageViewsCount(string itemId) { int counter = 0; using (var client = XConnectHelper.GetClient()) { try { IAsyncQueryable <Sitecore.XConnect.Contact> queryable = client.Contacts .Where(c => c.Interactions.Any(f => f.Events.OfType <PageViewEvent>().Any())) .WithExpandOptions(new Sitecore.XConnect.ContactExpandOptions() { Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions() { Limit = 2000 // Returns top 20 of all contact's interactions - interactions not affected by query } }); var enumerable = queryable.GetBatchEnumeratorSync(10); var urlList = new List <string>(); while (enumerable.MoveNext()) { foreach (var contact in enumerable.Current) { InteractionReference interactionReference = new InteractionReference((Guid)contact.Id, (Guid)contact.Interactions.ElementAt(0).Id); Interaction interaction = client.Get <Interaction>(interactionReference, new InteractionExpandOptions(new string[] { IpInfo.DefaultFacetKey }) { Contact = new RelatedContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey }) }); var pageViews = interaction.Events.OfType <PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList(); foreach (var page in pageViews) { // item id is in page if (page.ItemId.Equals(Guid.Parse(itemId))) { counter++; } urlList.Add(page.Url); } Console.WriteLine("Page Url: " + pageViews.FirstOrDefault().Url); } } //var count = urlList.GroupBy(x => x).Select(y => new { } x.Count()); var count = urlList.GroupBy(n => n).Select(c => new { Key = c.Key, total = c.Count() }); return(counter); } catch (XdbExecutionException ex) { } return(counter); } }
static void AssertQuery(IAsyncQueryable actual) { Assert.IsType <RewriteAsyncQuery <Dummy> >(actual); Assert.IsType <RewriteAsyncQueryProvider>(actual.Provider); var actualProvider = (RewriteAsyncQueryProvider)actual.Provider; Assert.IsType <Rewriter>(actualProvider.Rewriter); Assert.IsAssignableFrom <IAsyncQueryProvider>(actualProvider.Provider); }
/// <summary> /// Provides synchronous access to a collection resource. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An asynchronous data source to query synchronously.</param> /// <returns>A <see cref="IQueryable{T}"/> that will execute all requests synchronously.</returns> public static IQueryable <TSource> Synchronously <TSource>(this IAsyncQueryable <TSource> source) { var collection = source as CollectionResourceQueryable <TSource>; if (collection == null) { throw new InvalidOperationException("This queryable is not a supported collection resource."); } return(source as IQueryable <TSource>); }
/// <summary> /// Find ID values for all contacts that have not had any activity since a specified DateTime. /// If a bound is not provided, the logic assumes current time - 30 days. /// /// Based on documentation example: https://doc.sitecore.com/developers/92/sitecore-experience-platform/en/search-contacts.html /// </summary> /// <param name="cfg">The client configuration for connecting</param> /// <param name="lastActivity">The time to look for the contact's last activity</param> /// <returns>The matching contact object</returns> public virtual async Task <List <System.Guid> > GetContactIdsByLastActivity(XConnectClientConfiguration cfg, DateTime?lastActivity) { //Create a collection that will store the IDs of the results List <System.Guid> matchingContactIds = new List <System.Guid>(); //Establish a timebound to search for. If not provided, default to a value. DateTime searchStartTime = lastActivity.HasValue ? lastActivity.Value : DateTime.Now.AddDays(-30); Logger.WriteLine("Retrieving all Contacts without interactions since:" + searchStartTime.ToShortDateString()); // Initialize a client using the validated configuration using (var client = new XConnectClient(cfg)) { try { //Set up options to restrict the number of interactions we return for each contact var contactExpandOptions = new ContactExpandOptions(); contactExpandOptions.Interactions = new RelatedInteractionsExpandOptions() { Limit = 20 }; //Create a queryable to search by the date IAsyncQueryable <Contact> queryable = client.Contacts .Where(c => (!c.Interactions.Any()) || !c.Interactions.Any(x => x.StartDateTime > searchStartTime)) .WithExpandOptions(contactExpandOptions); //Invoke the query var enumerator = await queryable.GetBatchEnumerator(10); //Collect all the matching contact IDs while (await enumerator.MoveNext()) { foreach (var contact in enumerator.Current) { if (contact.Id.HasValue) { matchingContactIds.Add(contact.Id.Value); } } } Logger.WriteLine(">> Total number of matches found '{0}'", matchingContactIds.Count); } catch (XdbExecutionException ex) { // Deal with exception Logger.WriteError("Exception executing search", ex); } } return(matchingContactIds); }
public AsyncQueryableTest() { rewriter = new Rewriter(); query = Enumerable.Empty <Dummy>() .ToAsyncEnumerable() .AsAsyncQueryable() .OrderBy(d => d.Id); provider = new RewriteAsyncQueryProvider(query.Provider, rewriter); }
/// <summary> /// Converts a generic <see cref="IQueryable{T}"/> to a generic <see cref="IQueryableAndAsyncQueryable{T}"/> /// </summary> /// <param name="queryable"></param> /// <returns></returns> public static IQueryableAndAsyncQueryable <T> AsSyncAndAsyncQueryable <T>(this IAsyncQueryable <T> queryable) { if (queryable is IQueryableAndAsyncQueryable <T> r) { return(r); } else { return(new NonAsyncQueryableWrapper <T>(queryable)); } }
void ListBind() { List <ListViewModel> list = new List <ListViewModel>(); DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 0, 0); DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59); using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { var goalGuid = Guid.Parse("C22B6C5B-3065-46B4-8E10-A559AED540D2"); IAsyncQueryable <Sitecore.XConnect.Contact> queryable = client.Contacts .Where(c => c.Interactions.Any(f => f.Events.OfType <Goal>().Any(a => a.DefinitionId == goalGuid))) .Where(c => c.Interactions.Any(x => x.StartDateTime >= startDate.ToUniversalTime() && x.StartDateTime <= endDate.ToUniversalTime())) .WithExpandOptions(new Sitecore.XConnect.ContactExpandOptions() { Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions() { Limit = 100 // Returns top 100 of all contact's interactions - interactions not affected by query } }); var enumerable = queryable.GetBatchEnumeratorSync(100); while (enumerable.MoveNext()) { foreach (var contact in enumerable.Current) { Sitecore.XConnect.Collection.Model.EmailAddressList obj = contact.Emails(); list.Add(new ListViewModel { id = contact.Id.Value.ToString(), FirstName = contact.Personal().FirstName, LastName = contact.Personal().LastName, Email = obj.PreferredEmail.SmtpAddress.ToString() }); // Do something with contacts } } } catch (XdbExecutionException ex) { // Handle exception } } ListView1.DataSource = list; ListView1.DataBind(); }
/// <summary> /// Returns a specified number of contiguous elements from the start of a sequence. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">The sequence to return elements from.</param> /// <param name="count">The number of elements to return.</param> /// <returns>An <see cref="IAsyncQueryable{T}"/> that contains the specified number of elements from the start of <paramref name="source"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> public static IAsyncQueryable <TSource> Take <TSource>(this IAsyncQueryable <TSource> source, int count) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.Provider.CreateQuery( LinqHelper.MethodCall( LinqHelper.GetMethodInfo(Queryable.Take, (IQueryable <TSource>)null, count), source.Expression, Expression.Constant(count)))); }
/// <summary> /// Rewrite a given query. /// </summary> /// <typeparam name="T">The type of the query data.</typeparam> /// <param name="value">The query to rewrite.</param> /// <param name="rewriter">The rewriter to rewrite the query.</param> /// <returns>The rewritten query.</returns> public static IAsyncQueryable <T> Rewrite <T>(this IAsyncQueryable <T> value, ExpressionVisitor rewriter) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (rewriter == null) { throw new ArgumentNullException(nameof(rewriter)); } return(new RewriteAsyncQuery <T>(value, rewriter)); }
/// <summary> /// Asynchronously returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An <see cref="IAsyncQueryable{T}"/> to return the single element of.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The single element of the input sequence.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> /// <exception cref="System.InvalidOperationException"><paramref name="source"/> has more than one element.</exception> public static async Task <TSource> SingleAsync <TSource>(this IAsyncQueryable <TSource> source, CancellationToken cancellationToken = default(CancellationToken)) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (!await source.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { throw new InvalidOperationException("The sequence has no elements."); } return(source.CurrentPage.Single()); }
/// <summary> /// Asynchronously returns the only element of a sequence, or a default value if the sequence is /// empty; this method throws an exception if there is more than one element in the sequence. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An <see cref="IAsyncQueryable{T}"/> to return the single element of.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The single element of the input sequence, or <c>default(TSource)</c> if the sequence contains no elements.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception> /// <exception cref="System.InvalidOperationException"><paramref name="source"/> has more than one element.</exception> public static async Task <TSource> SingleOrDefaultAsync <TSource>(this IAsyncQueryable <TSource> source, CancellationToken cancellationToken = default(CancellationToken)) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (!await source.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { return(default(TSource)); } return(source.CurrentPage.SingleOrDefault()); }
/// <summary> /// Asynchronously iterates over the input sequence and performs the specified action on each element of the <see cref="IAsyncQueryable{T}"/>. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An <see cref="IAsyncQueryable{T}"/> containing items to operate on.</param> /// <param name="action">The action to perform on each element.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> public static Task ForEachAsync <TSource>(this IAsyncQueryable <TSource> source, Action <TSource> action, CancellationToken cancellationToken = default(CancellationToken)) { if (action == null) { throw new ArgumentNullException(nameof(action)); } return(source.ForEachAsync( item => { action(item); return false; }, cancellationToken)); }
private static IAsyncQueryable <NuGetPackage> Synchronized(IAsyncQueryable <NuGetPackage> async, Func <IQueryable <NuGetPackage>, IQueryable <NuGetPackage> > getNotEvaluated) { var syncTask = AsyncEnumerableExtensions.ToList(async); syncTask.Wait(); var completedList = syncTask.Result; var withFilter = getNotEvaluated(completedList.AsQueryable()); var completedEnumer = new CompletedAsyncEnumerator <NuGetPackage>(withFilter); return(AsyncEnumerable.FromResult(completedEnumer)); }
/// <summary> /// Create a new query to rewrite. /// </summary> /// <param name="query">The actual query.</param> /// <param name="provider">The provider to rewrite the query.</param> protected RewriteAsyncQueryable(IAsyncQueryable query, RewriteAsyncQueryProvider provider) { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (provider == null) { throw new ArgumentNullException(nameof(provider)); } Query = query; Provider = provider; }
/// <summary> /// Rewrite a given query. /// </summary> /// <typeparam name="T">The type of the query data.</typeparam> /// <param name="value">The query to rewrite.</param> /// <param name="rewriter">The rewriter to rewrite the query.</param> /// <returns>The rewritten query.</returns> public static IAsyncQueryable <T> AsyncRewrite <T>(this IAsyncQueryable <T> value, ExpressionVisitor rewriter) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (rewriter == null) { throw new ArgumentNullException(nameof(rewriter)); } var provider = new RewriteAsyncQueryProvider(value.Provider, rewriter); return(new RewriteAsyncQueryable <T>(value, provider)); }
/// <summary> /// Wraps an <see cref="IQueryable"/> as an <see cref="IQueryableAndAsyncQueryable"/> /// </summary> /// <param name="queryable"></param> /// <returns></returns> public static IQueryableAndAsyncQueryable AsSyncAndAsyncQueryable(this IAsyncQueryable queryable) { if (queryable is null) { throw new ArgumentNullException(nameof(queryable)); } else if (queryable is IQueryableAndAsyncQueryable r) { return(r); } else { return((IQueryableAndAsyncQueryable)_asAsyncQueryable2.MakeGenericMethod(queryable.ElementType).Invoke(null, new[] { queryable }) !); } }
/// <summary> /// Create a dynamic order by clause for a given property selector. /// </summary> /// <typeparam name="T">The type of the query data.</typeparam> /// <param name="query">The query to sort.</param> /// <param name="selector">The property selector to parse.</param> /// <param name="descending">True to sort descending, otherwise false.</param> /// <returns>The sorted query.</returns> public static IOrderedAsyncQueryable <T> OrderBy <T>(this IAsyncQueryable <T> query, string selector, bool descending = false) { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (string.IsNullOrEmpty(selector)) { throw new ArgumentNullException(nameof(selector)); } var target = Expression.Parameter(typeof(T)); return((IOrderedAsyncQueryable <T>)query.Provider.CreateQuery <T>(CreateAsyncOrderClause(target, query.Expression, selector, true, descending))); }