Back to Devexpress

QueryParameter Class

corelibraries-devexpress-dot-dataaccess-dot-mongodb-b5981754.md

latest5.4 KB
Original Source

QueryParameter Class

Contains methods and properties that allow you to conditionally filter data loaded from a MongoDB instance.

Namespace : DevExpress.DataAccess.MongoDB

Assembly : DevExpress.DataAccess.v25.2.dll

NuGet Package : DevExpress.DataAccess

Declaration

csharp
public sealed class QueryParameter :
    DataSourceParameterBase
vb
Public NotInheritable Class QueryParameter
    Inherits DataSourceParameterBase

Example

The example below demonstrates how to use the MongoDBDataSource class to bind an application or component to a MongoDB instance. The example uses the MongoDBCustomConnectionParameters class to specify a connection string to the MongoDB instance and the MongoDBQuery class to specify data queries to the Categories and Products collections of the Northwind database.

csharp
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
// ...
// Create a MongoDBCustomConnectionParameters object and assign
// a MongoDB connection string to the object's ConnectionString
// property.
var connectionParameters = new MongoDBCustomConnectionParameters() {
    ConnectionString = "mongodb://localhost:27017"
};

// Specify queries to database collections.
var queryParamLowPrice = new QueryParameter() {
    Name = "LowPrice",
    Type = typeof(int),
    Value = 10
};

var queryLowPriceProducts = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Products",
    Alias = "LowPriceProducts",
    FilterString = "[UnitPrice] <= ?LowPrice",
    Parameters = { queryParamLowPrice }
};

var queryParamHighPrice = new QueryParameter() {
    Name = "HighPrice",
    Type = typeof(int),
    Value = 40
};

var queryHighPriceProducts = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Products",
    Alias = "HighPriceProducts",
    FilterString = "[UnitPrice] >= ?HighPrice",
    Parameters = { queryParamHighPrice }
};

// Create a MongoDBDataSource object. Assign the created connection
// string to the object's ConnectionParameters property. Add the
// queries to the object's Queries collection.
var mongoDBDataSource = new MongoDBDataSource() {
    ConnectionParameters = connectionParameters,
    Queries = { queryLowPriceProducts, queryHighPriceProducts }
};

// Call the Fill method of the MongoDBDataSource object to execute the
// queries and load data from the MongoDB instance.
mongoDBDataSource.Fill();

// Use the created object as a data source in your application or component.
//...
vb
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.MongoDB
' ...
' Create a MongoDBCustomConnectionParameters object and assign
' a MongoDB connection string to the object's ConnectionString
' property.
Dim connectionParameters = New MongoDBCustomConnectionParameters() With {
    .ConnectionString = "mongodb://localhost:27017"
}

' Specify queries to database collections.
Dim queryParamLowPrice = New QueryParameter() With {
    .Name = "LowPrice",
    .Type = GetType(Integer),
    .Value = 10
}

Dim queryLowPriceProducts = New MongoDBQuery() With {
    .DatabaseName = "Northwind",
    .CollectionName = "Products",
    .Alias = "LowPriceProducts",
    .FilterString = "[UnitPrice] <= ?LowPrice"
}

queryLowPriceProducts.Parameters.Add(queryParamLowPrice)

Dim queryParamHighPrice = New QueryParameter() With {
    .Name = "HighPrice",
    .Type = GetType(Integer),
    .Value = 40
}

Dim queryHighPriceProducts = New MongoDBQuery() With {
    .DatabaseName = "Northwind",
    .CollectionName = "Products",
    .Alias = "HighPriceProducts",
    .FilterString = "[UnitPrice] >= ?HighPrice"
}

queryHighPriceProducts.Parameters.Add(queryParamHighPrice)

' Create a MongoDBDataSource object. Assign the created connection
' string to the object's ConnectionParameters property. Add the
' queries to the object's Queries collection.
Dim mongoDBDataSource = New MongoDBDataSource() With {
    .ConnectionParameters = connectionParameters
}

mongoDBDataSource.Queries.Add(queryLowPriceProducts)
mongoDBDataSource.Queries.Add(queryHighPriceProducts)

' Call the Fill method of the MongoDBDataSource object to execute the
' queries and load data from the MongoDB instance.
mongoDBDataSource.Fill()

' Use the created object as a data source in your application or component.
'...

Implements

IParameter

IFilterParameter

Inheritance

Object DataSourceParameterBase QueryParameter

See Also

QueryParameter Members

DevExpress.DataAccess.MongoDB Namespace