ILazyQueryable<TModel>Interface
.NET Standard 2.x
The ILazyQueryable<TModel>
interface represents an enumerable object that can be used to read the result set from database lazily.
It is used as the type of the returned value of ISqlExecutor.SelectLazy method.
After obtaining the required data, you need to call the ILazyQueryable<Model>.Dispose
method to release the resources occupied by the database connection and DataReader
. Before the returned ILazyQueryable<Model>
object is disposed, the current DataContext
cannot be used for other data operations. Otherwise, an exception will be thrown.
However, in some common cases, you can benefit from C# and do not have to explicitly call the ILazyQueryable<Model>.Dispose
method, for example, when using the C# using
statement or the C# foreach
statement, it can implicitly dispose ILazyQueryable<Model>
.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public interface ILazyQueryable<TModel> : IEnumerable<TModel>, IEnumerator<TModel>
Examples
The following code example demonstrates how to use the SelectLazy<TModel>(String, params object[])
method to execute the SQL statement and use the returned ILazyQueryable<TModel>
object to load data lazily.
using SnapObjects.Data;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Appeon.ApiDoc.ILazyQueryable_GenericExamples
{
public class ILazyQueryable_GenericExample
{
private readonly SchoolContext _context;
public ILazyQueryable_GenericExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public async Task Example()
{
// Defines the SQL statement.
var sql = @"SELECT CourseID, Title
FROM Course
WHERE CourseID = @Courseid";
// Coding style 1:
// Executes the SQL statement. It doesn't get the result set from database eagerly.
// This method is usually used to handle rows that contain columns
// with large binary values.
// Since 'using' can dispose result1 automatically, you don't need to dispose result1 explicitly.
await using (var result1 = _context.SqlExecutor.SelectLazy<DynamicModel>(sql, 2030))
{
// Gets each row of the result set from database one by one.
// It creates each model object and reads data from database to
// this model object only when the data is asked for.
await foreach (var model in result1)
{
Console.WriteLine("Result 1: {0}.", model.GetValue<string>(1));
}
}
// Coding style 2:
// Executes the SQL statement.
// It doesn't get the result set from database eagerly.
// This method is usually used to handle rows that contain columns
// with large binary values.
// If result2 hasn't been disposed, you need to dispose result2 explicitly.
var result2 = _context.SqlExecutor.SelectLazy<DynamicModel>(sql, 1050);
await foreach (var model in result2)
{
Console.WriteLine("Result 2: {0}.", model.GetValue<string>(1));
}
// Disposes result2 explicitly.
await result2.DisposeAsync();
/*This code produces the following output:
Result 1: Poetry.
Result 2: Chemistry.
*/
}
}
}
Applies to
.NET Standard
2.x