Deferred execution và lazy loading nó dùng tối ưu hóa các tập dữ liệu và truy xuất dữ liệu trong linq. Nếu ta truy vấn 1 giá trị đơn nó thực hiện luôn, nếu chúng ta có truy vấn các danh sách dữ liệu thì cơ chế này không trả về ngay lập tức, mà nó trì hoãn lại các thao tác trên tập dữ liệu cho đến khi chúng ta truy thật sự trê một dữ liệu cụ thể nào đó.
Code vidu hướng chi tiết Deferred execution và lazy loading
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace lazyloading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var datas = new List { “A, B,C,D,E” };
var dssp = from x in datas
select x;
// đoạn trên là truy hoãn thực hiện
datas.Add(“F”);
foreach (var y in dssp)
{
listBox1.Items.Add(y);
}
}
private void button2_Click(object sender, EventArgs e)
{
var dsint = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var dssole = from x in dsint
where x % 2 != 0
select x;
// đoạn trên là truy hoãn thực hiện
dsint.Add(113);
dsint.Add(114);
foreach (var y in dssole)
{
listBox1.Items.Add(y);
}
}
}
}