LINQ to object-operations on dictionary and SQL (left Outer Join \ group orderby sum)

Source: Internet
Author: User

Dictionary operation

 

C #

VaR vhe =NewDictionary <string, string> ();

VaR querystring = context. Request. querystring;

Foreach(StringItemInQuerystring ){

Vhe. Add (item, commonfunc. convertobjecttostring (querystring [item]);

}

ReturnVhe;
For the above C # CodeUse LINQ to object for coding

Linqtoobject

1:

VaR query = from itemInQuerystring. oftype <string> () SelectNewKeyvaluepair <String,String> (Item, querystring [item]); var vhe =NewDictionary <string, string> ();Foreach(VAR itemInQuery) {vhe. Add (item. Key. tostring (), item. value. tostring ());}
ReturnVhe;

2:

ReturnQuerystring. oftype <string> (). todictionary (item => item, item => querystring [item]);

 

 

Left Outer Join Operation

 

SQL:

 

Code

   Select    [  T0  ]  .  [  Memberid  ]  ,  [ T0  ]  .  [  Membername  ]   
From [ DBO ] . [ Memberinfo ] As [ T0 ]
Left Outer Join [ DBO ] . [ Membersort ] As [ T1 ] On ( [ T1 ] . [ Membertype ] = 0 ) And
( [ T0 ] . [ Memberid ] = [ T1 ] . [ Memberid ] )
Order By ( Case When [ T1 ] . [ Sortnumber ] Is Not Null Then [ T1 ] . [ Sortnumber ]
Else 9999
End )

 

LINQ:

 Code 
    VaR data  =  From m  In DB. memberinfo
Join s In DB. membersort on M. memberid equals S. memberid
Into X
From CX In X. Where (d => D. membertype = 0 ). Defaultifempty ()
Orderby Cx. sortnumber. hasvalue ? Cx. sortnumber. value: 9999
Select m;

 

 

Group orderby sum operation

 

SQL:

 

Code

   Select    [  Sid  ]  , CID,  Sum  (Visitcount ),  Sum (Resultcount ),  Sum  (Validcount)
From DBO. countvisit
Where Reportdate Between ' 2010-03-04 ' And ' 2010-03-08 ' And
[ Sid ] Like ' % M % '
Group By [ Sid ] , CID
Order By [ Sid ] , CID

 

LINQ:

Code

   VaR list =     New  List  <  Countvisitentity  >  ();

Using (VAR TB = Getcountvisitbyparam (svcp, Ref Flippageoption )){

If (Tb. Rows. Count > 0 ){

VaR Query = From item In TB. asenumerable ()
Group item New {Sid = Item. Field < String > ( " Sid " ),
CID = Item. Field < String > ( " CID " )
} Into result
Orderby result. Key. Sid, result. Key. CID
Select New Countvisitentity (){
Sid = Result. Key. Sid,
CID = Result. Key. CID,
Xx = Result. sum (o => O. Field < Int32 > ( " Xx " )),
Xxx = Result. sum (o => O. Field < Int32 > ( " Xxx " )),
Xxxx = Result. sum (o => O. Field < Int32 > ( " Xxxx " ))
};
List = Query. tolist ();
}
}

Return List;

 

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.