To Select Records by Using LINQ
ToList
or ToArray
method for the query, or iterate
the query by using the foreach
statement. For example, the following
code executes the query defined by a LINQ
expression.//query is a LINQ expression
var data = query.ToList();
For details about the execution of LINQ expressions, see Deferred LINQ Query Execution.
Before You Proceed
- Add the
using
directives shown below to your code.using PX.Data; using PX.Data.SQLTree; using System.Linq;
- Make sure that the application database has the database tables from which you are going to request data, and that the application defines the data access classes (DACs) for these tables. For more information on defining DACs, see Data Access Classes.
To Filter Records
To filter records in the database table to be retrieved, construct the LINQ
expression by using the Where
LINQ method and the needed
conditions. In the conditions, use the property field defined in the DAC, such as
Product.ProductID.
ProductMaint graph = PXGraph.CreateInstance<ProductMaint>();
IQueryable<Product> query = graph.Select<Product>().Where(prod =>
prod.BookedQty > prod.AvailQty
|| prod.AvailQty < prod.MinAvailQty
|| prod.AvailQty == null);
[list of columns]
is the list of columns of the
Product
table.
SELECT [list of columns] FROM Product
WHERE Product.BookedQty > Product.AvailQty
OR Product.AvailQty < Product.MinAvailQty
OR Product.AvailQty IsNull
To Order Records
To order records to be retrieved from the database table, construct the LINQ
expression by using the OrderBy
or
OrderByDescending
LINQ method and the needed property fields of
the DAC, such as Product.ProductID.
The following sample LINQ expression selects all Product data records and sorts them by the UnitPrice field in ascending order.
ProductMaint graph = PXGraph.CreateInstance<ProductMaint>();
IQueryable<Product> query = graph.Select<Product>().OrderBy(prod => prod.UnitPrice)
.ThenByDescending(prod => prod.AvailQty);
[list
of columns]
is the list of columns of the Product
table.SELECT [list of columns] FROM Product
ORDER BY Product.UnitPrice, Product.AvailQty DESC
To Query Multiple Tables
To join multiple tables, construct the LINQ expression by using the
Join
, LeftJoin
, GroupJoin
,
and FullJoin
LINQ methods and the needed property fields of DACs,
such as SalesOrder.OrderNbr.
The following sample LINQ expression performs an inner join of the SalesOrder and OrderDetail DACs by the OrderNbr field.
SalesOrderEntry graph = PXGraph.CreateInstance<SalesOrderEntry>();
var query = graph.Select<SalesOrder>()
.Join(graph.Select<OrderLine>(),
ord => ord.OrderNbr, ordDet => ordDet.OrderNbr,
(ord, ordDet) => new { SalesOrder = ord, OrderDetail = ordDet });
[list
of columns]
is the list of columns of the Product
table.SELECT [list of columns] FROM SalesOrder
INNER JOIN OrderDetail
ON OrderDetail.OrderNbr = SalesOrder.OrderNbr
To Group or Aggregate Records
- Construct the LINQ expression by using the
GroupBy
LINQ method and the needed property fields of the DAC, such as Product.CategoryCD. (The name of the property field starts with an uppercase letter. Do not confuse it with the class field, which has the same name but starts with lowercase letter.) - Append to the expression the Select LINQ method.Note:You have to use the Select method after
GroupBy
to eliminate the number of requests that the system performs to the database.
The following sample LINQ expression groups the records of the
Product
table by the CategoryCD
field.
ProductMaint graph = PXGraph.CreateInstance<ProductMaint>();
var query = graph.Select<Product>().GroupBy(prod => prod.CategoryCD).
Select(group => new { CategoryCD = group.Key });
SELECT Product.CategoryCD
FROM Product
GROUP BY Product.CategoryCD
To Select Particular Columns of Records
To select particular columns, specify the corresponding property fields of DACs in
the Select
clause.
ProductCD
and
AvailQty
fields for all records of the Product
table.ProductMaint graph = PXGraph.CreateInstance<ProductMaint>();
var results = graph.Select<Product>()
.Select(p => new { ProductCD = p.ProductCD, AvailQty = p.AvailQty });
SELECT Product.ProductCD, Product.AvailQty FROM Product