C# : LINQ

[".0s4*0|collections;datetime-format;linq",["F@eOQTU^","GAECAVBEMA","OOCCSTUUUUTTUUUUYGDGDGDGDGDGDFOCBEOBBWSTTUUUUTTUUUU","ehwry.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","GroupBy"," transforms a collection into groups. Each group has a key. With this method from the System.Linq namespace, you can do this in your C# program on many collections. We examine the GroupBy method on a collection of numbers.","Example."," We are using the method syntax available in the System.Linq namespace. We call GroupBy with the argument being a lambda expression. Each element is identified as 'a' in the lambda expression (a => IsEven(a)). ","Then: ","The key becomes the result of IsEven, which is a boolean value. Thus the result is two groups with the keys True and False.","Results: ","The group with the key of value False contains the odds. And the group with the key of value True, meanwhile, contains the evens.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses GroupBy method","\n\nusing System;\nusing System.Linq;\n\nclass Program\n{\n static void Main()\n {","\n // Input array.\n ","int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };","\n\n // Group elements by IsEven.\n ","var result = array.GroupBy(a => IsEven(a));","\n\n // Loop over groups.\n ","foreach (var group in result)\n {","\n // Display key for group.\n ","Console.WriteLine(\"IsEven = {0}:\", group.Key);","\n\n // Display values in group.\n ","foreach (var value in group)\n {\n Console.Write(\"{0} \", value);\n }","\n\n // End line.\n ","Console.WriteLine();\n }\n }\n\n static bool IsEven(int value)\n {\n return value % 2 == 0;\n }\n}\n\n","Output","\n\nIsEven = False:\n1 3 5 7 9\nIsEven = True:\n2 4 6 8","Discussion."," Using the GroupBy method (or the equivalent query) is fine for certain parts of programs. However, if you are generating a collection that will be repeatedly used, it would probably be better to use ToDictionary instead. ","And: ","While GroupBy can index elements by keys, a Dictionary can do this and has the performance advantages provided by hashing.","Group By ","group","ToDictionary ","todictionary","If each group"," contains multiple elements as is typical with GroupBy, you could use a List as the value of the Dictionary instance. Thus you would have a Dictionary of Lists. This is an efficient design. ","Dictionary ","dictionary","List ","list","Summary."," We looked at the GroupBy extension method in the C# programming language. Further, we discussed how the GroupBy method can be used in away similar to a Dictionary of Lists and noted the possible performance drawbacks. ","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAABiCAMAAADgMSigAAAAGFBMVEX////evu/06vnm5/nm1PT1/v/WpOns/v9gQvwOAAADaUlEQVR4Xu2b4WrEMAyDJ9tp3/+Nt4MSFkS/48JKOqh+VndFsa1UzW1fnyPi655o+w9K2w+Uy8W0tmeWtHVif0HbAbV1wiKrpEOIBn1bxwKFXcSA+t1f/Wa0SGCOAvOcikUuMBVWwAO1QKCrsOav7PHuAgUCt9t4ZL1AMIIXt0N38EgDru7gEa7u+hEUqK8beaSzWrnJNOhi52XS13rEP5GlbVN15au3acb6EbydwLyhQI4yjPVRBnArj7QXNKB+UVEDol/PKvT78MGqdAkbe4TGkwbjdTX13mrt+GCH0msEIgK2cEjarjlo6kdYakePAAeDYYuxIg6rRgR6ZC5p167NUOBJQuMoAyrOB0NQinGgtA3Tt0fv0jUeYTTM7nEykRxlPnwbZRTr8xK998inb6MdlXYzHqc48Rt7RJNvo+Ul8onZZTJ2sOmER2AwThqis2YVSSAfABcwGKelPVkUdEPT23SDwWCBzoTV4VKP1BcOlC9XuI+gD6D0dFzHAr2AWCPyAXEwGLgqb4egUeyRhNKTR0AgFNCYy6IMTDxM4CEwrEbgA+A023z/RvGzFso76ZHAFbsKa+LVUabBNkgW4WOD9mdxnz3ilxNrNHd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