IDataStore.LastOrDefault<TModel>(Predicate<TModel> predicate) Method
.NET Standard 2.x
Generic method. Returns the last data row of the DataStore that meets the criteria specified by Predicate<TModel>
. If no data row is found, returns null
.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public TModel LastOrDefault<TModel>(Predicate<TModel> predicate);
Type Parameters
TModel
The type of a model
class that matches with the current DataObject
.
Parameters
predicate
System.Predicate<TModel>
A Predicate<TModel>
that you want to use as the search criteria.
Returns
TModel
Returns the TModel
object which contains the data of the last row that meets the search criteria.
Returns null
if no row is found.
Examples
The following code example demonstrates how to get the last record object in DataStore that meets the criteria.
using Appeon.ApiDoc.Models;
using DWNet.Data;
using System;
namespace Appeon.ApiDoc.IDataStoreExamples
{
public class LastOrDefaultExample
{
private readonly SchoolContext _context;
public LastOrDefaultExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example2()
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
datastore.Retrieve();
// The first record in the Department table is:
// departmentid =1, name=Engineering
// The second record in the Department table is:
// departmentid =2, name=English
// The third record in the Department table is:
// departmentid =4, name=Economics
// The last record in the Department table is:
// departmentid =7, name=Mathematics
// The following code gets the D_Department object
// whose departmentid is larger than 1.
var department = datastore.LastOrDefault<D_Department>(
d => d.Departmentid > 1);
Console.WriteLine("Department ID: {0}; Department Name: {1}",
department.Departmentid,
department.Name);
// The department table has no record whose Departmentid is larger than 100.
department = datastore.LastOrDefault<D_Department>(
d => d.Departmentid > 100);
if (department == null)
{
Console.WriteLine("The department is null.");
}
else
{
Console.WriteLine("Department ID: {0}; Department Name: {1}",
department.Departmentid, department.Name);
}
/*This code produces the following output:
Department ID: 7; Department Name: Mathematics
The department is null.
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x