LINQ to SQL 第五集 - 如何比對日期欄位的資料

取得訂單日期為 1998 年的資料,當然也可以比對 月(Month)  或  日(Day)。

using System;
using System.Linq;
using System.Windows.Forms;

namespace LinqToSql
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            NorthwindDataContext db = new NorthwindDataContext();

            var q = from o in db.Orders
                           where o.OrderDate.Value.Year == 1998
                           select o;

            dataGridView1.DataSource = q.ToList();
        }
    }
}

執行結果:

image

Enjoy.