Chưa phân loại

Hướng dẫn OfType trong linq c# lọc đối tượng theo yêu cầu

OfType trong linq c# là một phương thức dùng để lọc 1 đối tượng thõa mãn 1 điều kiện tho yêu cầu nào đó.

OfType trong linq c#

Code hướng dẫn thực hành tìm lọc danh sách nhân viên đã ký hợp đồng vào nhân viên thử việc.


Để làm bài này chúng ta tạo ra 3 lớp kế thừa nhau:
+ Lớp nhân viên
+ Lớp nhân viên chính thức
+ Lớp nhân viên thời vụ

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace HocOftype

{

   public  class NhanVien

    {

        public string MaNV { get; set; }

        public string TenNV { get; set; }

        public string SoHD { get; set; }

        public virtual  Double TinhLuong()// virtual phương thức ảo trong hàm

        {

            return 8000000; // Lương cơ bản

        }

    }

}

 //Nhân viên chính thức thừa kế lớp nhân viên

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace HocOftype

{

   public  class NhanVienchinhthuc: NhanVien

    {

        public override double TinhLuong()

        {

            return base.TinhLuong()+1000000;

        }

    }

}

Nhân viên thời vụ thừa kế lớp nhân viên.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace HocOftype

{

  public   class NhanVienThoiVu: NhanVien

    {

        public override double TinhLuong()

        {

            return 200000;

        }

    }

}

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 HocOftype

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        List<NhanVien> DSnhanVien = new List<NhanVien>();

        private void button1_Click(object sender, EventArgs e)

        {

            NhanVien nv = null;

            if (ckThuViec.Checked)

            {

                nv = new NhanVienThoiVu();

            }

            else

            {

                nv = new NhanVienchinhthuc();

            }

            nv.MaNV = txtMa.Text;

            nv.TenNV = txtTen.Text;

            nv.SoHD = txtHD.Text;

            DSnhanVien.Add(nv);

            lvNhanVien.Items.Clear();

            foreach (var x in DSnhanVien)

            {

                ListViewItem lvi = new ListViewItem(x.MaNV);

                lvi.SubItems.Add(x.TenNV);

                lvi.SubItems.Add(x.SoHD);

                lvNhanVien.Items.Add(lvi);

                if (x is NhanVienThoiVu)

                {

                    lvi.ForeColor = Color.Red;

                }

                else

                {

                    lvi.ForeColor = Color.Blue;

                }

            }

        }

        private void btnLocchinh_Click(object sender, EventArgs e)

        {

            var dsCT = DSnhanVien.OfType<NhanVienchinhthuc>();

            lvDsDaloc.Items.Clear();

            foreach (var nv in dsCT)

            {

                ListViewItem lvi = new ListViewItem(nv.MaNV);

                lvi.SubItems.Add(nv.TenNV);

                lvi.SubItems.Add(nv.SoHD);

                lvDsDaloc.Items.Add(lvi);

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            var dsTV = DSnhanVien.OfType<NhanVienThoiVu>();

            lvDsDaloc.Items.Clear();

            foreach (var nv in dsTV)

            {

                ListViewItem lvi = new ListViewItem(nv.MaNV);

                lvi.SubItems.Add(nv.TenNV);

                lvi.SubItems.Add(nv.SoHD);

                lvDsDaloc.Items.Add(lvi);

            }

        }

    }

}

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *