Skipwhile trong linq loại bỏ phần tử đầu tiên thõa mãn điều kiện
Skipwhile loại các bỏ phần từ đầu tiên thõa mãn điều kiện. Đối với skipwhile chỉ cần gặp 1 điều kiện sai thì nó dừng luôn.
Code hướng dẫn thực hành skipwhile. Skipwhile sẽ tìm phần tử đầu tiên không thõa mãn điều kiện đi tiếp nếu thõa mãn điều kiện thì sẽ dừng lại.
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 Hocskywhile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
List<int> dsint = new List<int>();
Random rd = new Random();
private void btnTaoDS_Click(object sender, EventArgs e)
{
int n = int.Parse(txtNhap.Text);
for (int i = 0; i < n; i++)
{
dsint.Add(rd.Next(50));
}
lstSo.Items.Clear();
dsint.ForEach(x => lstSo.Items.Add(x));
}
private void btnskipwhile_Click(object sender, EventArgs e)
{
int Skwhile = int.Parse(txtskipwhile.Text);
var dsskwhile = dsint.SkipWhile(x => x > Skwhile).ToList();
lstDSloc.Items.Clear();
dsskwhile.ForEach(x => lstDSloc.Items.Add(x));
}
}
}
Không có nhận xét nào