资源描述:
《linq在java中的开源介绍》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、LINQforJavaIntegratingQuery,SetsandTransformoperationsinJ2EEOlmodelCorralLINQforJavaTheLINQProjectisasetofextensionstothe.NETFrameworkforlanguage-integratedquery,set,andtransformoperations.ExtendsC#widthnativelanguagesyntaxandclasslibrariesforqueries.We
2、willshowawaytodoitinJavawithoutmakinganychangeintheJavalanguage.QueryExampleThisisthewayaC#3Querylooklike.Itsorts,groupandproyectthealreadydefinedcollection:IEnumerablepeople;SeemsaSQLQuery,butisjustsyntaxsugar.Soyoucandothesamewithoutusingitvar
3、query2=frompinpeoplewherep.Age>20orderbyp.Agedescending,p.Namegroupnew{p.Name,Senior=p.Age>30,FirstJob=p.Jobs[0]}byp.CanCode;WithoutQueryExpressionsWithoutthecosmetics,itstartsfeelingstandardJavacode.Inthe5thline,Name=isomitted.Thecompilerdoitforyou.But
4、itmakesconfusing.Itisworth?varquery2=people.Where(p=>p.Age>20).OrderByDescending(p=>p.Age).ThenBy(p=>p.Name).GroupBy(p=>p.CanCode,p=>new{p.Name,Senior=p.Age>30,FirstJob=p.Jobs[0]});WithoutAnonymousFieldsinObjectInicializatorsAstepfurtherincodenormaliza
5、tion.But,whatnew{…}means??It’saAnonymousType.JustlikeaJavaAnonymousClassbutonlywithFields,Properties…Thecorrectwayofdoingit’sbybuildingyourownclass.varquery2=people.Where(p=>p.Age>20).OrderByDescending(p=>p.Age).ThenBy(p=>p.Name).GroupBy(p=>p.CanCode,p=
6、>new{Name=p.Name,Senior=p.Age>30,FirstJob=p.Jobs[0]});WithoutAnonymousTypesNowwehavePersonResumeclasswithawell-knownsyntax!Again,whatkindofconstructorhavebracesinsteadof()?It’saObjectInizializator,thisexpressioncallsthedefaultconstructorandtheniteratess
7、ettingeveryinvolverproperty.InJavaweshouldcreateaspecificconstructor:classPersonResume{string_name;bool_senior;Job_firstJob;stringName{get{return_name;}set{_name=value;}}boolSenior{get{return_senior;}set{_senior=value;}}JobFirstJob{get{return_firstJob;}
8、set{_firstJob=value;}}}varquery2=people.Where(p=>p.Age>20).OrderByDescending(p=>p.Age).ThenBy(p=>p.Name).GroupBy(p=>p.CanCode,p=>newPersonResume{Name=p.Name,Senior=p.Age>30,FirstJob=p.Jobs[0]});WithoutObjectsInicializatorsPersonR