0% found this document useful (0 votes)
63 views3 pages

LinQ trong lập trình C# Winform - How Kteam-trang-3-6

The document describes using LINQ in a C# Windows forms application to filter a list of food items. It defines a Food class with Name and Price properties, loads an initial list with sample foods, and binds it to a combo box. A search button uses LINQ's OrderBy and ToList methods to sort the list by price and display the results in another combo box.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views3 pages

LinQ trong lập trình C# Winform - How Kteam-trang-3-6

The document describes using LINQ in a C# Windows forms application to filter a list of food items. It defines a Food class with Name and Price properties, loads an initial list with sample foods, and binds it to a combo box. A search button uses LINQ's OrderBy and ToList methods to sort the list by price and display the results in another combo box.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12:35 23/02/2023 LinQ trong lập trình C# Winform | How Kteam

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 namespace LinQGUI
12 {
13 public partial class Form1 : Form
14 {
15 List<Food> foodList;
16 public Form1()
17 {
18 InitializeComponent();
19 LoadFood();
20 }
21
22 void LoadFood()
23 {
24 foodList = new List<Food>();
25 foodList.Add(new Food("Dú dê nướng", "55555"));
26 foodList.Add(new Food("Mực một nắng nướng muối ớt", "999999"));
27 foodList.Add(new Food("Ếch núp nùm", "333333"));
28 foodList.Add(new Food("cái gì đây", "1"));
29 foodList.Add(new Food("7up", "15000"));
30 foodList.Add(new Food("Ốc mỡ cháy tỏi", "50000"));
31
32 cbData.DataSource = foodList;
33 cbData.DisplayMember = "Name";
34 }
35
36 private void btnSearch_Click(object sender, EventArgs e)
37 {
38 List<Food> result = new List<Food>();
39
40 //foreach (Food item in foodList)
41 //{
42 // if (item.Name == txbKey.Text)
43 // {
44 // result.Add(item);
45 // }
46 //}
47
48 //result = foodList.Where(p => p.Name == txbKey.Text).ToList();
49 //var result2 = foodList.Select(p=>p.Price).SingleOrDefault();
50
51 //result = foodList.Skip(2).Take(2).ToList();
52 result = foodList.OrderBy(p=>Convert.ToInt32(p.Price)).ToList();
53
54 cbResult.DataSource = result;
55 cbResult.DisplayMember = "Name";
56 }
57 }
58
59 public class Food
60 {
61 private string price;
62
63 public string Price
64 {
65 get { return price; }
66 set { price = value; }
67 }
68 private string name;
69
70 public string Name
71 {
72 get { return name; }
73 set { name = value; }
74 }
75
76 public Food() { }
77
78 public Food(string name, string price)

79 {
80 this.Name = name;
https://howkteam.vn/course/lap-trinh-winform-co-ban/linq-trong-lap-trinh-c-winform-1312 3/7
12:35 23/02/2023 LinQ trong lập trình C# Winform | How Kteam

1 namespace LinQGUI
2 {
3 partial class Form1
4 {
5 /// <summary>
6 /// Required designer variable.
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// Clean up any resources being used.
12 /// </summary>
13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows Form Designer generated code
24
25 /// <summary>
26 /// Required method for Designer support - do not modify
27 /// the contents of this method with the code editor.
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.cbData = new System.Windows.Forms.ComboBox();
32 this.btnSearch = new System.Windows.Forms.Button();
33 this.txbKey = new System.Windows.Forms.TextBox();
34 this.cbResult = new System.Windows.Forms.ComboBox();
35 this.SuspendLayout();
36 //
37 // cbData
38 //
39 this.cbData.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
40 this.cbData.FormattingEnabled = true;
41 this.cbData.Location = new System.Drawing.Point(12, 46);
42 this.cbData.Name = "cbData";
43 this.cbData.Size = new System.Drawing.Size(134, 215);
44 this.cbData.TabIndex = 0;
45 //
46 // btnSearch
47 //
48 this.btnSearch.Location = new System.Drawing.Point(118, 10);
49 this.btnSearch.Name = "btnSearch";
50 this.btnSearch.Size = new System.Drawing.Size(75, 23);
51 this.btnSearch.TabIndex = 1;
52 this.btnSearch.Text = "Filter";
53 this.btnSearch.UseVisualStyleBackColor = true;
54 this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
55 //
56 // txbKey
57 //
58 this.txbKey.Location = new System.Drawing.Point(12, 12);
59 this.txbKey.Name = "txbKey";
60 this.txbKey.Size = new System.Drawing.Size(100, 20);
61 this.txbKey.TabIndex = 2;
62 //
63 // cbResult
64 //
65 this.cbResult.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
66 this.cbResult.FormattingEnabled = true;
67 this.cbResult.Location = new System.Drawing.Point(152, 46);
68 this.cbResult.Name = "cbResult";
69 this.cbResult.Size = new System.Drawing.Size(120, 215);
70 this.cbResult.TabIndex = 3;
71 //
72 // Form1
73 //
74 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
75 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
76 this.ClientSize = new System.Drawing.Size(284, 261);
77 this.Controls.Add(this.cbResult);
78 this.Controls.Add(this.txbKey);

79 this.Controls.Add(this.btnSearch);
80 this.Controls.Add(this.cbData);
https://howkteam.vn/course/lap-trinh-winform-co-ban/linq-trong-lap-trinh-c-winform-1312 5/7
12:35 23/02/2023 LinQ trong lập trình C# Winform | How Kteam

81 this.Name = "Form1";
82 this.Text = "Form1";
83 this.ResumeLayout(false);
84 this.PerformLayout();
85
86 }
87
88 #endregion
89
90 private System.Windows.Forms.ComboBox cbData;
91 private System.Windows.Forms.Button btnSearch;
92 private System.Windows.Forms.TextBox txbKey;
93 private System.Windows.Forms.ComboBox cbResult;
94 }
95 }
96
97

Download project

 Kết luận
Bài sau chúng ta sẽ cùng tìm hiểu về Entity Framework trong lập trình C# Winform.

Cảm ơn các bạn đã theo dõi bài viết. Hãy để lại bình luận hoặc góp ý của mình để phát triển bài viết tốt hơn. Đừng quên “Luyện tập – Thử thách –
Không ngại khó”.

 Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên
thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.

CỘNG ĐỒNG HỎI ĐÁP HOWKTEAM.COM

GROUP THẢO LUẬN FACEBOOK

TÁC GIẢ/DỊCH GIẢ

HowKteam

KHÓA HỌC

Lập trình Winform cơ bản

Lập trình Winform cơ bản

ĐÁNH GIÁ

5 3
5.0 4

3
 3 đánh giá
2
Đánh giá
1

0932110332 đã đánh giá 2 năm trước




 Báo cáo

hthecong đã đánh giá 3 năm trước

 

https://howkteam.vn/course/lap-trinh-winform-co-ban/linq-trong-lap-trinh-c-winform-1312 6/7

You might also like