XperienceCommunity.DevTools.QueryExtensions 2.0.1

dotnet add package XperienceCommunity.DevTools.QueryExtensions --version 2.0.1                
NuGet\Install-Package XperienceCommunity.DevTools.QueryExtensions -Version 2.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XperienceCommunity.DevTools.QueryExtensions" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XperienceCommunity.DevTools.QueryExtensions --version 2.0.1                
#r "nuget: XperienceCommunity.DevTools.QueryExtensions, 2.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XperienceCommunity.DevTools.QueryExtensions as a Cake Addin
#addin nuget:?package=XperienceCommunity.DevTools.QueryExtensions&version=2.0.1

// Install XperienceCommunity.DevTools.QueryExtensions as a Cake Tool
#tool nuget:?package=XperienceCommunity.DevTools.QueryExtensions&version=2.0.1                

This package provides a set of extension methods for Xperience by Kentico ContentTypeQueryParameters, ContentItemQueryBuilder, WhereParameters, ObjectQuery, and ConnectionHelper's returned IDataReader / DbDataReader data access APIs.

Dependencies

This package is compatible with ASP.NET Core 8 applications or libraries integrated with Xperience by Kentico Versions 29.5.2 +.

How to Use?

  1. Install the NuGet package in your ASP.NET Core project (or class library)

    dotnet add package XperienceCommunity.DevTools.QueryExtensions
    
  2. Add the correct using to have the extensions appear in intellisense

    using XperienceCommunity.QueryExtensions.ContentItems;

    using XperienceCommunity.QueryExtensions.Objects;

    using XperienceCommunity.QueryExtensions.Collections;

    The extension methods are all in explicit namespaces to prevent conflicts with extensions that Xperience might add in the future or extensions that the developer might have already created.

    You can apply these globally with C# implicit usings

Extension Methods

ObjectQuery

Prerequisites
using XperienceCommunity.QueryExtensions.Objects;
Examples
return UserInfo.Provider.Get()
    .Tap(q =>
    {
        // access the query
    });
bool condition = ...

var query = UserInfo.Provider.Get()
    .If(condition, q => 
    {
        // when condition is true
    });
bool condition = ...

var query = UserInfo.Provider.Get()
    .If(condition, 
    q => 
    {
        // when condition is true
    }, 
    q =>
    {
        // when condition is false
    });
var query = UserInfo.Provider.Get()
    .OrderByDescending(nameof(UserInfo.UserLastModified))
    .TopN(1)
    .DebugQuery();

/*
--- BEGIN [path\to\your\app\Program.cs] QUERY ---


SELECT TOP 1 *
FROM CMS_User
ORDER BY UserLastModified DESC


--- END [path\to\your\app\Program.cs] QUERY ---
*/
var query = UserInfo.Provider.Get()
    .OrderByDescending(nameof(UserInfo.UserLastModified))
    .TopN(1)
    .DebugQuery("User");

/*
--- QUERY [User] START ---


SELECT TOP 1 *
FROM CMS_User
ORDER BY UserLastModified DESC


--- QUERY [User] END ---
*/
public void QueryDatabase(ILogger logger)
{
    var query = UserInfo.Provider.Get()
        .OrderByDescending(nameof(UserInfo.UserLastModified))
        .TopN(1)
        .LogQuery(logger, "Logged User Query");
}
var query = UserInfo.Provider.Get()
    .TapQueryText(text =>
    {
        // do something with the query text
    });
var query = UserInfo.Provider.Get()
    .Source(s => s.InnerJoin<UserSettingInfo>(
        "UserID", 
        "UserSettingUserID", 
        "MY_ALIAS",
        additionalCondition: new WhereCondition("MY_ALIAS.UserWaitingForApproval", QueryOperator.Equals, true),
        hints: new[] { SqlHints.NOLOCK }))
    .TopN(1)
    .DebugQuery("User");

/*
--- QUERY [User] START ---


SELECT TOP 1 *
FROM CMS_User
INNER JOIN CMS_UserSetting AS MY_ALIAS WITH (NOLOCK) ON UserID = MY_ALIAS.UserSettingUserID AND MY_ALIAS.UserWaitingForApproval = 1
ORDER BY UserLastModified DESC


--- QUERY [User] END ---
*/
// ExecuteAsync returns a populated dataset with all the columns returned by the query.
// When there are no results, dataset.Tables[0] will still be populated with an empty DataTable.

var dataset = await UserInfo.Provider.Get()
    .Source(source => source
        .InnerJoin<UserSettingInfo>(
            "UserID", 
            "UserSettingUserID", 
            "MY_ALIAS")
        .InnerJoin<CustomerInfo>(
            "CustomerUserID",
            "UserID",
            "C",
            )
        )
    .Columns("UserID", "UserSettingID", "CustomerID")
    .ExecuteAsync();
    
foreach (var row in dataset.Tables[0].Rows)
{
    Console.WriteLine($"User: {row["UserID"]}, User Setting: {row["UserSettingID"]}, Customer: {row["CustomerID"]}");
}

XperienceCommunityConnectionHelper

Examples
var dataSet = await XperienceCommunityConnectionHelper.ExecuteQueryAsync("CMS.User", "GetAllUsersCustom");
string queryText = @"
SELECT *
FROM CMS_User
WHERE UserID = @UserID
"

var queryParams = new QueryDataParameters
{
    { "UserID", 3 }
};

var dataSet = await XperienceCommunityConnectionHelper.ExecuteQueryAsync(queryText, queryParams, token: token);

References

.NET

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 85 10/30/2024
2.0.0 96 10/29/2024