LINQ 101--Grouping, Set, transform, Element

Source: Internet
Author: User

First, Grouping (group)

Example 1: Grouping 0-9 numbers by 3 evenly divisible results

Code:

1 Static voidLinq1 ()2 {3     int[] numbers = {5,3,2,4,0,7,8,6,9,1 };4     varNumModBy3 = fromNinchnumbers5Group N by n%36 Into G7                         Select New{remainder = G.key, Numbers =g};8 9     foreach(varGinchnumModBy3)Ten     { OneConsole.Write ("is divisible by 3 by the remainder {0}:", G.remainder); A         foreach(varNuminchg.numbers) -         { -Console.Write ("{0}", num); the         } - Console.WriteLine (); -     } -}
View Code

Example 2: Grouping words by first letter

Code:

1 Static voidLinq2 ()2 {3     string[] Words = {"Blueberry","Chimpanzee","Abacus","Banana","Apple","Cheese" };4     varGroupeddata = fromWinchwords5Group W by w[0]6 Into G7                              byG.key//Add Sort8                             Select New{Firstchar = G.key, Words =g};9     foreach(varGinchgroupeddata)Ten     { OneConsole.Write ("starting with the {0} letter are:", G.firstchar); A         foreach(varWordinchg.words) -         { -Console.Write ("{0}", word); the         } - Console.WriteLine (); -     } -}
View Code


Second, set (set)

Distinct to Heavy

Source. Distinct ();

Other unions, Intersect, Except see LINQ 101 Sample


Iii. Conversion (conversion)

ToList
ToDictionary


Iv. Element (Elemental operation)

First
FirstOrDefault

LINQ 101--Grouping, Set, transform, Element

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.