Entity Framework Learning VI: Some common query techniques

Source: Internet
Author: User

var people == = ==== = = = = = = = =new{p. Lastname,p.firstname,p.persontype.typename}); foreach (var in people) {Console.WriteLine ("{0} {1} {2}"  , person. FirstName, person. LastName, person. TypeName);}
 var  people = context. People.where (P  = p.personstate == personstate.active). (P  => P.lastname). ThenBy (P  => P.firstname). Select (P  = new  {p.lastname,p. Firstname,p.persontype.typename});  foreach  (var  person in   people) {Console.WriteLine (  {0} {1} {2}   " ,person. FirstName, person. LastName, person. TypeName);}  
var explicitquery = from in context. Peoplewhere oneperson.personstate = = personstate.active;
 Public classpersoninfo{ Public intPersonId {Get;Set; } Public stringPersonType {Get;Set; } Public stringFirstName {Get;Set; } Public stringLastName {Get;Set; }}
varExplicitquery = fromOnepersoninchcontext. PeoplewhereOneperson.personstate = =personstate.active byOneperson.lastname, Oneperson.firstnameSelect NewPersoninfo{lastname=Oneperson.lastname,firstname=Oneperson.firstname,persontype=Oneperson.persontype.typename,personid=Oneperson.personid,phones= OnePerson.Phones.Select (ph=>Ph. PhoneNumber)};foreach(varPersoninchexplicitquery) {Console.WriteLine ("{0} {1} {2} {3}", person. FirstName, person. Lastname,person. PersonType, person. PERSONID);foreach(varPhoneinchPerson . Phones) {Console.WriteLine (" "+phone);}}

Min

var min = context. People.min (p = p.birthdate);

Count

var min = context. People.min (p = p.birthdate);
 var  query = from  Oneperson in   context. People  where  oneperson.personstate == select  new  Span style= "color: #000000;" >{oneperson.heightinfeet,oneperson.personid};query  = query. (P => P.heightinfeet);  var  sum = query. Sum (P => P.heightinfeet); Console.WriteLine (sum);  
 var  query = from  Oneperson in   context. People  where  oneperson.personstate == select  new  Span style= "color: #000000;" >{oneperson.heightinfeet,oneperson.personid};query  = query. (P => P.heightinfeet);  var  sum = query. Sum (P => P.heightinfeet); Console.WriteLine (sum);  
 from inch context. Peoplewhere2) >new DateTime (1970,1,1) Select New {Oneperson.heightinfeet,oneperson.personid};

Paging:

var people ===1) * criteria. PageSize). Take (criteria. PageSize);

Using join:

 var  people = from  person in   context. Peoplejoin persontype   context. Persontypeson person. Persontypeid equals Persontype.persontypeid  select  new  { var  people = from  person in   context. Peoplejoin persontype   context. Persontypeson person. Persontypeid equals Persontype.persontypeid  select  new  { 
People == = = ==new= =new{p. Person.lastname,p.person.firstname,p.persontype.typename});

Grouping and left connections:

 from  Oneperson in   context. Peoplegroup Oneperson by  new   select  new  {month  = MonthGroup.Key.Month, Count  = Monthgroup.count ()};  
 var  methodquery =context. People.groupby (Oneperson  = new   => Monthgroup). Select (Monthgroup  = new  {month  = Monthgroup.key.month,count  = 
 from  person in   context. Peoplejoin persontype   context. Persontypeson person. Persontypeid Equalspersontype.persontypeid into Finalgroup  from  Groupeddata in   Finalgroup.defaultifempty ()  select  new  {person. Lastname,person. Firstname,typename  = groupeddata.typename??  " unknown   " };  
 var  methodquery = context. People.groupjoin (context. Persontypes,person  => person. Persontypeid,persontype  => Persontype.persontypeid, (person, type)  = > new  {person  =  Person,persontype  = type}). SelectMany (groupeddata  =>groupeddata.persontype.defaultifempty (), (group , persontype)  = new  {group. Person.lastname,group. Person.firstname,typename  = persontype.typename??  " unknown   "  
 from  Oneperson in   context. People  from  onephone in   Oneperson.phones  orderby   Oneperson.lastname, Onephone.phonenumber  select  new  {oneperson.lastname,oneperson.firstname,onephone.phonenumber};  
var methodquery == = person. Phones, (person, phone) =new== p.phonenumber);

Distinct

var uniquequery == = = p.persontype.typename). Distinct ();

Union:

 var  unionquery = context. People.select (P  = new  {name  = P.lastname +  "  + P.firstname,rowtype  =  person  Span style= "color: #800000;" > " }). Union (Context.Companies.Select (c  = new  {name  = C.companyname,rowtype  = "  company    => result. RowType). ThenBy (Result  = = result. Name); 

Entity Framework Learning VI: Some common query technologies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.