iT邦幫忙

DAY 30
6

看範例學C#系列 第 30

看範例學C#-30 LINQ查詢範例

LINQ查詢範例
LINQ是一種標準且容易學習的查詢運算模式,它能讓您對記憶體中的集合或資料表進行 蒒選、巡覽,可以利用LINQ來對物件集合、SQL Server資料庫、ado.net資料集與xml文件等進行查詢動作,以下為本例程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ex30
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string msg = "考試及格成績:\n";
            // LINQ資料來源為陣列物件集合
            int[] scores = new int[] { 48 , 44 , 47 , 49 , 94 ,
                                       83 , 46 , 60 , 79 , 64 , 100};

            // 定義查詢運算式
            var scoreQuery =
                from score in scores
                where score >= 60
                orderby score
                select score;

            // 執行查詢
            foreach (var obj in scoreQuery)
            {
                msg = msg + obj + " ";
            }
            MessageBox.Show(msg, "[物件集合]查詢運算式");
        }
    }
}


全系列文章列表


上一篇
看範例學C#-29 閒置過久自動關閉畫面
系列文
看範例學C#30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
krarm
iT邦好手 1 級 ‧ 2011-10-27 23:13:01

恭喜練成

krarm iT邦好手 1 級 ‧ 2011-10-27 23:13:40 檢舉

這30篇都是以後我要好好follow的好文

0
draguitar
iT邦新手 4 級 ‧ 2020-02-07 15:19:23

非常實用的C#範例教學

我要留言

立即登入留言