IT SHARE EDU
- 1 Findall là một phương thức dùng để trả về các đối tượng cùng thỏa mãng 1 tính chất nào đó. Khác so với Hàm Find trả về một đối tượng thỏa mãng mà thôi. Findall có đối tường nào cùng thõa mãng tính chất thì nó trả về tất cả. Ứng dụng tìm kiếm sản phẩm hết hạn hoặc giá sản phẩm…
- 2 Video hướng dẫn làm bài tập FindAll chi tiết
Findall là một phương thức dùng để trả về các đối tượng cùng thỏa mãng 1 tính chất nào đó. Khác so với Hàm Find trả về một đối tượng thỏa mãng mà thôi. Findall có đối tường nào cùng thõa mãng tính chất thì nó trả về tất cả. Ứng dụng tìm kiếm sản phẩm hết hạn hoặc giá sản phẩm…
Code hướng dẫn sử dụng findall tìm lọc số chẵn và số lẻ đối tập dữ liệu kiểu int. và tìm lọc giá sản phẩm kiểu string.
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 hoc_FindAll
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<SanPham> dssp = new List<SanPham>();
private void Form1_Load(object sender, EventArgs e)
{
dssp = new List<SanPham>();
dssp.Add(new SanPham() { Ma = ” SP1″, Ten = “Ken”, DonGia = 20 });
dssp.Add(new SanPham() { Ma = ” SP2″, Ten = “333”, DonGia = 12 });
dssp.Add(new SanPham() { Ma = ” SP3″, Ten = “Sai Gon”, DonGia = 15 });
dssp.Add(new SanPham() { Ma = ” SP4″, Ten = “Tiger”, DonGia = 9 });
dssp.Add(new SanPham() { Ma = ” SP5″, Ten = “coca”, DonGia = 16 });
dssp.Add(new SanPham() { Ma = ” SP6″, Ten = “Pepsi”, DonGia = 25 });
dssp.Add(new SanPham() { Ma = ” SP7″, Ten = “Nước Suối”, DonGia = 5 });
dssp.Add(new SanPham() { Ma = ” SP8″, Ten = “Bia Hà Nội”, DonGia = 10 });
dssp.Add(new SanPham() { Ma = ” SP9″, Ten = “redbull”, DonGia = 15 });
dssp.Add(new SanPham() { Ma = ” SP10″, Ten = “Sprite”, DonGia = 12 });
dssp.ForEach(x =>
{
ListViewItem lvi = new ListViewItem(x.Ma);
lvi.SubItems.Add(x.Ten);
lvi.SubItems.Add(x.DonGia+””);
lvSanPham.Items.Add(lvi);
}
);
}
List<int> dsInt = new List<int>();// danh sach int
Random rd = new Random();
private void btnTaoDS_Click(object sender, EventArgs e)
{
int n = int.Parse(txtNhaSo.Text);
dsInt = new List<int>();
for (int i = 0; i < n; i++)
{
dsInt.Add(rd.Next(100));
}
lstSo.Items.Clear();
dsInt.ForEach(x => lstSo.Items.Add(x));
}
private void button1_Click(object sender, EventArgs e)
{
List<int> dschan = dsInt.FindAll(x => x % 2 == 0);
lstChan.Items.Clear();
dschan.ForEach(x => lstChan.Items.Add(x));
}
private void button2_Click(object sender, EventArgs e)
{
List<int> dsSoLe = dsInt.FindAll(x => x % 2 != 0);
lstChan.Items.Clear();
dsSoLe.ForEach(x => lstChan.Items.Add(x));
}
private void button3_Click(object sender, EventArgs e)
{
int min = int.Parse(txtTu.Text);
int max = int.Parse(txtDen.Text);
List<SanPham> dsloc = dssp.FindAll(x => x.DonGia >= min && x.DonGia <= max);
lvSanPhamLoc.Items.Clear();
dsloc.ForEach(x =>
{
ListViewItem lvi = new ListViewItem(x.Ma);
lvi.SubItems.Add(x.Ten);
lvi.SubItems.Add(x.DonGia + “”);
lvSanPhamLoc.Items.Add(lvi);
}
);
}
}
}
Tạo lớp SanPham:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hoc_FindAll
{
public class SanPham
{
public string Ma { get; set; }
public string Ten { get; set; }
public int DonGia { get; set; }
}
}
Video hướng dẫn làm bài tập FindAll chi tiết