LinQ操做

 

 

1、LinQ to SQLhtml

LinQ是指集成化查詢語言,經過映射將數據庫內的表名變爲C#的類名,將列名做爲屬性名,將表的關係做爲類的成員對象。O--M--R數據庫

O-Object對象 R-Relation關係 M-Mapping映射app

LinQ包括:LinQ to SQL,針對SQL的查詢,它是一個帶有可視化的操做界面的ORM工具。框架

               LinQ to Object,用來實現對內存中集合對象的查詢。函數

               LinQ to DataSet,對強類型化或弱類型化的DataSet或獨立的DataTable進行查詢。工具

               LinQ to Entity,對實體框架中EDM定義的實體集合進行查詢。post

(一):LinQ to SQLthis

第一步:創建LinQ2SQL類spa

添加一個新建項,選擇LinQ to SQL,將用到的表拖進來。code

第二步:實例化上下文對象,基本全部操做都是經過上下文對象來進行的。

MyDBDataContext context = new MyDBDataContext();

第三步:操做

1、增:

1.造對象。

            Info data = new Info();

            data.Code = "p211";

            data.Name = "石頭";

            data.Sex = false;

            data.Nation = "N001";

            data.Birthday = new DateTime(1990, 1, 2);

2.在上下文中註冊上面新造的對象。

            context.Info.InsertOnSubmit(data);

3.提交

            context.SubmitChanges();

2、刪:

1. 找出來

            MyDBDataContext context = new MyDBDataContext();

            var q = context.Info.Where(p => p.Code == "p003");

2. 註冊

 

            if (q.Count() > 0)

            {

                Info data = q.First();

                //註冊

                context.Work.DeleteAllOnSubmit(data.Work);

                context.Family.DeleteAllOnSubmit(data.Family);

                context.Info.DeleteOnSubmit(data);

 

3. 提交

                context.SubmitChanges();

3、改:

1. 找出來

            MyDBDataContext context = new MyDBDataContext();

            var q = context.Info.Where(p=>p.Code == "p001");

2. 改,不用註冊

 

            if (q.Count() > 0)

            {

                Info data = q.First();

                //改

                data.Name = "胡生";

                data.Nation = "n001";

                data.Sex = false;

 

3. 提交

           context.SubmitChanges();

4、查:

1.查全部

 

            MyDBDataContext context = new MyDBDataContext();

            //全部人員

            var q = context.Info;             //var q = from p in context.Info select p;

            //顯示

            foreach(Info data in q)

            {

                //data.Nation1:當前人員對應的民族對象。

                Console.WriteLine(data.Name+"\t"+data.Nation1.Name);

                //data.Work:當前人員對應的工做記錄集合

                foreach (Work work in data.Work)

                {

                    Console.WriteLine("\t"+work.Firm+"\t"+work.Depart);

                }

            }

 

2. 根據主鍵查詢(條件查詢),默認返回的都是集合。

var q = from p in context.Info where p.Code == "p211" select p;

var q = context.Info.Where(p => p.Code == "p211").Where(p => p.Nation1.Name == "漢族"); //Lambda表達式(最最簡化的函數,只有參數和函數體的匿名函數)

var q = context.Info.Where(p => p.Code == "p211" && p.Nation1.Name=="漢族");

var q = context.Info.Where(p => p.Code == "p211"); //默認返回的是集合

if (q.Count() > 0)    //看看集合中是否查出數據來了

{

    Info data = q.First(); //取第一個對象出來

    Console.WriteLine(data.Nation1.Name + data.Name);

}

3. 多條件查詢

var q = from p in context.Car where p.Price > 30&& p.Brand=="b002" select p;

var q = context.Car.Where(p => p.Price > 30 && p.Brand == "b002");

var q = context.Car.Where(p => p.Price > 30).Where(p => p.Brand == "b002");

 

var q = from p in context.Car where p.Price > 30 || p.Brand == "b002" select p;

var q = context.Car.Where(p => p.Price > 30 || p.Brand == "b002");

4. 模糊查詢

 

var q = from p in context.Car where p.Name.Contains("5") select p; //包含

var q = from p in context.Car where p.Name.StartsWith("奧迪") select p;//開頭

var q = from p in context.Car where p.Name.EndsWith("奧迪") select p;//結尾

var q = context.Car.Where(p => p.Name.Contains("5"));//包含

var q = context.Car.Where(p => p.Name.StartsWith("奧迪"));//開頭

var q = context.Car.Where(p => p.Name.EndsWith("型"));//結尾

var q = from p in context.Car where p.Name.Substring(2, 1) == "5" select p;//第三個字符

var q = context.Car.Where(p => p.Name.Substring(2,1) == "5");//第三個字符是5

 

5. Distinct查詢

var q = (from p in context.Car select p.Brand).Distinct();//去重

var q = context.Car.Select(p=>p.Brand).Distinct();//去重

6. 鏈接查詢——對象之間的關聯關係點出來的。

var q = context.Car.Where(p => p.Brand1.Productor.Prod_Name=="一汽豐田");

7. 去重查詢出顯示,IEnumerable是集合的根類型

 

        static void Show(IEnumerable<string> q)

        {

            foreach (string d in q)

            {

                Console.WriteLine(d);

            }

        }

 

 

8. 統計查詢

 

            MyDBDataContext context = new MyDBDataContext();

            //var query = from p in context.Car select p;

            var query = context.Car;//查出全部

            Console.WriteLine(query.Count());//統計個數

            Console.WriteLine(query.Max(p => p.Price));//價格的最大值

            Console.WriteLine(query.Min(p => p.Price));//價格的最小值

            Console.WriteLine(query.Max(p => p.Price * p.Oil));//價格乘以油耗的最大值

            Console.WriteLine(query.Sum(p => p.Price));//價格求和

            Console.WriteLine(query.Sum(p => p.Price * p.Oil));//價格乘以油耗以後求和

            Console.WriteLine(query.Average(p => p.Price));//價格平均值

 

9. 分頁

            //分頁

            MyDBDataContext context = new MyDBDataContext();

            var query = context.Car.Skip(2 * 4).Take(4);//每頁4條數據,要查第三頁的4條數據,跳過8條取下面4條

            Show(query);

10. 排序

            //排序

            MyDBDataContext context = new MyDBDataContext();

            //var query = context.Car.OrderBy(p=>p.Price); //升序

            var query = context.Car.OrderByDescending(p => p.Price);//降序

            Show(query);

11. 集合操做

 

            //集合操做

            MyDBDataContext context = new MyDBDataContext();

            var query1 = context.Car.Where(p => p.Brand1.Productor.Prod_Name == "一汽豐田");

            var query2 = context.Car.Where(p => p.Price > 30);

 

            //交集

            var query = query1.Intersect(query2);

            //並集

            //var query = query1.Union(query2);

            //差集

            //var query = query1.Except(query2);

            //補集

            //var query = context.Car.Except(query1);

 

            Show(query);

 

 2、linq to object

            List<data_KeMu.KeMu> list = data.SelectAll();             data_KeMu.KeMu sj = new data_KeMu.KeMu();             //添加「請選擇」 sj.KeMucode = "all";             sj.KeMuname = "請選擇"; list.Insert(0,sj);插入指定位置0;

eg .list 集合的linq 操做

複製代碼
 1 //搜索按鈕  2 private void btn_Search_Click(object sender, EventArgs e)  3 {  4 //定義的初始的list數據  5 List<data__ShiTi.ShiTi> list = new List<data__ShiTi.ShiTi>();  6 //List<data__ShiTi.ShiTi> list1 = new List<data__ShiTi.ShiTi>();  7 data__ShiTi.ShiTiDA da = new data__ShiTi.ShiTiDA();  8 list = da.SelectAll();  9 10 11 if (coBox_kemu.Text != "請選擇") 12 { 13 //List<data__ShiTi.ShiTi> li = da.Select_kemu(coBox_kemu.SelectedValue.ToString()); 14 //list = list.Intersect(li,new ).ToList(); 15 string s = coBox_kemu.SelectedValue.ToString(); 16 17 //判斷list集合中p.KeMucode的相同的部分 18 list = list.Where(p => p.KeMucode == s).ToList(); 19 } 20 if (coBox_nanyi.Text != "請選擇") 21 { 22 //List<data__ShiTi.ShiTi> li = da.Select_nanyi(coBox_nanyi.SelectedValue.ToString()); 23 24 string s = coBox_nanyi.SelectedValue.ToString(); 25 list = list.Where(a => a.Easycode == s).ToList(); 26 } 27 if (coBox_tixing.Text != "請選擇") 28 { 29 //List<data__ShiTi.ShiTi> li = da.Select_tixing(coBox_tixing.SelectedValue.ToString()); 30 //list.Intersect(li).ToList(); 31 32 string s = coBox_tixing.SelectedValue.ToString(); 33 list = list.Where(a => a.TiMucode == s).ToList(); 34 } 35 if (tBox_key.Text.ToString() != "") 36 { 37 //string s = da.select_tmcontent(tBox_key.Text); 38 //list = list.Where(c => c.TiMucontent == s).ToList(); 39 //利用lambda表達式中的p.TiMucontents.Contains();函數來求是否包含 40 string s=tBox_key.Text; 41 list = list.Where(p => p.TiMucontent.Contains(s)).ToList(); 42 } 43 dataGridView_disp1.DataSource = list; 44 }
複製代碼

 

3、擴展屬性

複製代碼
 1 //擴展屬性 科目名稱  2 private string keMuname;  3 public string KeMuname  4  {  5 get  6  {  7 ShiTiDA da = new ShiTiDA();  8 return da.KeMuName(keMucode);//KeMuName(string s);屬於自定函數方法  9  } 10 set { keMuname = value; } 11  } 12 13 //擴展難易度 名稱 14 private string codename; 15 public string Codename 16  { 17 get 18  { 19 ShiTiDA da = new ShiTiDA(); 20 return da.EasyName(easycode); 21  } 22 set { codename = value; } 23  } 24 25 //擴展題目類型 26 private string tiMuname; 27 public string TiMuname 28  { 29 get 30  { 31 ShiTiDA da = new ShiTiDA(); 32 return da.TypeName(tiMucode); 33  } 34 set { tiMuname = value; } 35 } 
相關文章
相關標籤/搜索