黑龙江快乐十分

LINQ 笔记

8/3/2015来源:C#应用人气:16748

LINQ 笔记

zailinqkaifadangzhongvarguanjianzixiandetebiedezhongyao,dangnishengmingyigevarleixingdebianliangshihou,xitongshibuzhidaodangqiandeleixing,zhiyoudangnidiyiciweiqifuzhideshihou,huigenjuqizhilaishezhixiangyingdeshujuleixing,zailinqchaxuntongchangbuzhidaofanhuideleixing,suoyijiangchaxundaodejieguofangdaovarleixingdebianliangdangzhong 。

biru:

var myint=0;//zaibianyideshihouhuizidongjiangqizhuanchengintleixing

黑龙江快乐十分 var mybool=true;//bianyideshihouhuijiangqizhuanchengboolleixing

黑龙江快乐十分 var mystring=”ime,marcheson”//bianyishizidongjiangqizhuanchengstringleixing

class lownums

{

static void main()

{

// a simple data source.

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

// create the query.

黑龙江快乐十分 // lownums is an ienumerable<int>

ienumerable lownums = from num in numbers

where num < 5

select num;

// execute the query.

foreach (int i in lownums)

{

console.write(i + " ");

}

}

}

tongguoshangmianhongsebiaojideneixingjiukeyikanchubiaozhundelinqyujutongchangbaohanfrom in where selectzheijigeguanjianzi,dajiashibushiganjuegensqldeyujuhenxiang,danshitangruonizhennasqldeyujulaiyongyidinghuifeichangaonao,yinweitakansiyiyangdanshijishangqueshiwanquanbutongdeliangzhongbiaozhun,women jianglinqchaxuncaozuofu,tongguobiaogedexingshiliechulai,ruxia。

chaxuncaozuofu

hanyi

from 、in

yongyudingyirenhelinqbiaodashidezhugan,yunxucongheshidechuangkouzhongtiqushujuziji

where

yongyudingyicongyigerongqiliquchunaxiexiangdexianzhitiaojian

select

yongyucongrongqizhongxuanzeyigexulie

join、on、equals、into

jiyuzhidingdejianlaizuoguanjiancaozuo。jizhu,zheixie“guanlian”bubiyuguanxideshujuyouguanxi

ordderby、ascending、descending

yunxujieguozijianshengxuhejiangxupaixu

group、by

yongtedingdezhilaiduishujufenzuhoudedaoyigeziji

shangmiandetongchang shichuangjianyigechaxun,chaxundejieguoweishaoyu5dexiang,bingqieduiqijinxingshuchu 。

zaijinxingxiamiandezhishizhiqianyidingyaoxianlejieienumerableheforeachzheilianggejiandandegainian。ienumerableqishizaic#shiyigejiekou,zaizheigejiekoulimiandingyileyigefangfagetenumerator,zheigefangfadezuoyongshifanhuiyigexunhuanfangwenjihedemeijuqi。zheijuhuashenmeyiyini?jiushidangnidemouyigeduixiangjichengyuzheigejiekou,neimezheigeduixiangjiukeyitongguoforeachlaijinxingbianli,meicibianlijiuxiangdangyudiaoyonggetenumeratorduqudangqianzuobiaodezhibingqiedingweidaoxiayigezuobiaodeweizhi。ruwomenchangyongdearrayhelistqishidoushijichenglezheigejiekoude,neimezhiyaodingyizheilianggeduixiangdeshihouxuandingxiangyingdeshujuleixing,bingkeyizhijietongguoforeachlaibianlizuneideyuansu。

黑龙江快乐十分henduoshihouwomenbuzhidaofanhuideshujuleixing,neimezuihaodebianfashijiangqidingyichengvarleixing,neimeyonghuzaitongguoforeachbianlideshihouyejiangqigaichengvarleixing

黑龙江快乐十分class lownums

{

static void main()

{

// a simple data source.

黑龙江快乐十分 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

黑龙江快乐十分 // create the query.

黑龙江快乐十分 // lownums is an ienumerable<int>

黑龙江快乐十分 var lownums = from num in numbers

where num < 5

黑龙江快乐十分 select num;

// execute the query.

黑龙江快乐十分 foreach (var i in lownums)

{

黑龙江快乐十分 console.write(i + " ");

}

}

}

linqzijufanhuideshienumerableduixiang,suoyidangniyonglinqchaxundeshihou,keyizaiqiwaibudiaoyongenumerableleidekuozhanfangfa。biruxiamiandechaxunkeyitongguodiaoyongtoarray<int>()hetolist<int>fangfajiangchaxundaodejieguo,zhuanhuanchengxiangyingleixingderongqidangzhong 。

int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 };

int[] subsetasintarray = (from i in numbers where i < 10 select i).toarray<int>();

黑龙江快乐十分 list<int>subsetaslistofints=(from i in numbers where i<10 select i).tolist<int>();

linqchulecongjiandandeshuzuzulichouchujieguoyiwai,yekeyicaozuosystem.collections.genericmingmingkongjianzhongdechengyuanleixingdeshuju,biruwomendingyiyigecarlei。

黑龙江快乐十分public class car

{

public string petname { get; set; }

public string color { get; set; }

public int speed { get; set; }

public string make { get; set; }

}

黑龙江快乐十分jinjiezhewomenzaimainhanshulimian,chuangjianyigelistjihe,jihedeleixingweicar.

黑龙江快乐十分 list<car> mycars=new list<car>(){

黑龙江快乐十分 new car{petname="henry",color="silver",speed=100,make="bmw"},

new car{petname="daisy",color="tan",speed=90,make="bmw"},

new car{petname="mary",color="black",speed=55,make="vw"},

黑龙江快乐十分 new car{petname="clunker",color="rust",speed=5,make="yugo"},

new car{petname="melvin",color="white",speed=43,make="ford"}

};

zheiliwomenjiukeyitongguolinqduiqijinxingcaozuo,biruwomenkeyikeyichaxunqispeeddayudengyu55xiaoyudengyu100,bingqiemakeweibmwdezhi,womenkeyitongguoyixiayujulaichaxun;

var fastcars = from c in mycars where c.speed >= 55 &&c.speed<=100&& c.make == "bmw" select c;

黑龙江快乐十分 foreach(var car in fastcars)

{

console.writeline(car.petname);

}

zheiliwomenfaxianduifanxingrongqideshiyonggenjiandandeshuzumeiyoushenmequbie,zhiqianwomentandaofanxinghuchuli,neimeshifoufeifanxingdebunengchuli,daandangranbushi,zaic#limiantigongyigeoftype<t>()fangfalaijiangdangqianhuduixiangzhuanhuachengyigeyigejianrongyuienumerable<t>deduixiang。

static void linqoverarraylist()

{

黑龙江快乐十分 console.writeline("***** linq over arraylist*****");

黑龙江快乐十分 arraylist mycars = new arraylist()

{

new car{petname="henry",color="silver",speed=100,make="bmw"},

new car{petname="daisy",color="tan",speed=90,make="bmw"},

黑龙江快乐十分 new car{petname="mary",color="rust",speed=55,make="vw"}

};

var mycarsenum=mycars.oftype<car>();

var fastcars = from c in mycarsenum where c.speed >= 55 select c;

foreach(var car in fastcars)

{

黑龙江快乐十分 console.writeline("{0},is going to fast!", car.petname);

}

}

duibiaodashijinxingpaixu

chaxunbiaodashizhongwomentongguoorderbylaiduishujujinxingpaixu,paixumorenanzhengxujinxingpaixu。nikeyitongguoascendingcaozuofuhaolaizhidingweizhengxupaixu,yekegaiweidescendinglaijinxingnixupaixu。

zhengxu:

var subset=from p in oducts orderby p.name ascending select p;

nixu:

var subset=from p in products orderby p.name descending select p;

黑龙江快乐十分enumerablehaitigongleyigekuozhanfangfa,keyiduiliangge(huoduoge)linqchaxundeshujujinxinghebing(union)bijiao(difference)、lianjie(concatenation)hejiaocha(intersection)。

except()kuozhanfangfa,tafanhuihanlianggerongqibutongzhichudelinqjiheji。xiamiandehanshufanhuidezhiyouyige”yugo”

static void displaydiff()

{

黑龙江快乐十分 list<string>mycars=new list<string>{"yugo","aztec","bmw"};

list<string> yourcars = new list<string> { "bmw", "saab", "aztec" };

var cardiff = (from c in mycars select c).except(from c2 in yourcars select c2);

console.writeline("here is what ou don't have,but i do:");

黑龙江快乐十分 foreach (string s in cardiff)

console.writeline(s);

}

黑龙江快乐十分intersect()fangfafanhuilianggerongqizhonggongtongdeshujuxiang。jiangshangmiandechengxuxiugaiyixia,fanhuidejiangshi”aztec”、”bmw”

static void displayintersection()

{

黑龙江快乐十分 list<string> mycars = new list<string> { "yugo", "aztec", "bmw" };

list<string> yourcars = new list<string> { "bmw", "saab", "aztec" };

var cardiff = (from c in mycars select c).intersect(from c2 in yourcars select c2);

console.writeline("here is what ou don't have,but i do:");

foreach (string s in cardiff)

console.writeline(s);

}

黑龙江快乐十分union()shijiangduogejieguohebingchengyige,ruguoyouxiangtongdechengyuanzhifanhuiyigejieguo。xiamiandechengxufanhuideshiyugo、aztec、bmw、saab

static void displayunion()

{

list<string> mycars = new list<string> { "yugo", "aztec", "bmw" };

黑龙江快乐十分 list<string> yourcars = new list<string> { "bmw", "saab", "aztec" };

var cardiff = (from c in mycars select c).union(from c2 in yourcars select c2);

console.writeline("here is what ou don't have,but i do:");

foreach (string s in cardiff)

Console.Wr


优质网站模板

  • 1
  • 2
  • 3