void dataBindByWhere()
??????? {
??????????? /**************select/distinct 練習*******************/
??????????? //獲取數據庫中的T_Student表數據
??????????? var list = from s in ctx.T_Student
?????????????????????? select s;
??????????? //采用匿名類型
??????????? var list1 = from s in ctx.T_Student
??????????????????????? select new
??????????????????????? {
??????????????????????????? stuNumber = s.stuNumber,
??????????????????????????? stuSex = s.stuSex,
??????????????????????????? //這里邊是可以計算的
??????????????????????????? stuAge = s.stuAge / 2 + s.stuAge / 2,
??????????????????????????? //也可以進行條件判斷
??????????????????????????? stuAgeTime = s.stuAge>20?"成年":"年輕",
??????????????????????????? stuName = s.stuName
??????????????????????? };
??????????? //也可以不用匿名類型獲取個別字段值
??????????? var list2 = from s in ctx.T_Student
??????????????????????? select s.stuName;
??????????? //也可以轉化為自定義的類型,比如這里的peple為自定義的類型
??????????? var list3 = from s in ctx.T_Student
??????????????????????? select new People
??????????????????????????? {
??????????????????????????????? stuNumber = s.stuNumber,
??????????????????????????????? stuName = s.stuName
??????????????????????????? };
??????????? //可以在select 匿名類型里邊繼續包括匿名類型
??????????? var list4 = from s in ctx.T_Student
??????????????????????? select new
??????????????????????? {
??????????????????????????? stuNumber = s.stuNumber,
??????????????????????????? stuName = s.stuName,
??????????????????????????? stuInfo = new T_Student { stuAge = s.stuAge, stuSex = s.stuSex }
??????????????????????? };
??????????? //也可以繼續在select 里邊包括select
??????????? var list5 = from s in ctx.T_Student
??????????????????????? select new {
??????????????????????????? stuNumber = s.stuNumber,
??????????????????????????? stuSCore = from c in s.T_Score
?????????????????????????????????????? where s.stuNumber==c.stuNumber
?????????????????????????????????????? select c.score
??????????????????????? };
??????????? //也可以在select 里邊調用方法
??????????? var list6 = from s in ctx.T_Student
??????????????????????? select new {
??????????????????????????? stuNumber = s.stuNumber,
??????????????????????????? stuName = s.stuName,
??????????????????????????? stuAge = AgeInfo(s.stuAge)
??????????????????????? };
??????????? //也可以對查詢結果去除重復
??????????? var list7 = (from s in ctx.T_Student
???????????????????????? select s.stuName).Distinct();
??????????? GridView1.DataSource = list;
??????????? GridView1.DataBind();
??????? }
??????? string AgeInfo(int age)
??????? {
??????????? return age > 20 ? "成年" : "年輕";
??????? }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
