As you may know, Microsoft is getting into the open source community with the ASP.NET and DNX with the mono project.
The .NET Execution Environment (DNX) is a software development kit which allows you to run .NET applications on Windows, Mac, and Linux using different frameworks such as .NET framework, .NET Core, and Mono. However, everything are not roses. There are many incomplete libraries, incompatibilities, lack of documentation, and most of the examples such as doing SQL or SOAP do not work depending on which the library targets you are planning to code for. Therefore, I decided to test the basics on all the library targets. I am starting with DNX451.

Language Integrated Query (LINQ) seems to be working fine in DNX451 with C#. Below is a code example for those who wish to play with it.

Program.cs

using System;

namespace ConsoleApp1 {
	
	public class Program {
		
		public void Main(string[] args){			
			Console.WriteLine("Main()");
			
			displayWhichDnxIsCompilingOn();
			
			new LINQExample.LINQExample();
			
		}
		
		private void displayWhichDnxIsCompilingOn(){
			#if DNX451
			Console.WriteLine("Compiled on DNX451: .NET Framework");
			#endif
			
			#if DNXCORE50
			Console.WriteLine("Compiled on DNXCORE50: .NET Core 5");
			#endif						
		}
		
	}
	
}
LINQExample.cs

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExample {
	
	public class LINQExample {
	
		public LINQExample(){
			Console.WriteLine("\nLINQExample()");
			
			var sample = "This is a text example to show how LINQ works";
			
			var result = from s in sample 
						 select s;
						 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");
			
			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 orderby s // ascending by default
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");

			result = from s in sample.ToLower()
					 where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 orderby s descending
					 select s;
					 
			foreach (var item in result){
				Console.Write(item + "-");
			}
			
			Console.WriteLine("\n------");

			var result2 = from s in sample.ToLower()
					 	  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					 	  group s by s;					 
					 
			foreach (var item in result2){
				Console.WriteLine(item);
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.Write(item.Key + "-");
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  orderby s descending
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.Write(item.Key + "-");
			}
			
			Console.WriteLine("\n------");

			result2 = from s in sample.ToLower()
					  where s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u'
					  orderby s 
					  group s by s;					 
					 
			foreach (var item in result2){
				Console.WriteLine(item.Key + ":" + item.Count());
			}
			
			Console.WriteLine("\n------");
			
			var people = new List<Person>{
				new Person { FirstName = "Alex", LastName = "Carlstein", Age = 33 },
				new Person { FirstName = "Diego", LastName = "Carlstein", Age = 32 },
				new Person { FirstName = "Alex", LastName = "Veron", Age = 40 },
				new Person { FirstName = "Vanina", LastName = "Veron", Age = 34 },
				new Person { FirstName = "Bob", LastName = "Smith", Age = 12 },
			};
			
			var result3 = from p in people
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");
			
			result3 = from p in people
						  where p.Age < 35
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");
			
			result3 = from p in people
						  where p.Age < 35 && p.LastName == "Carlstein"
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0} {1}: {2}", item.FirstName, item.LastName, item.Age);
			}

			Console.WriteLine("\n------");

			result3 = from p in people
						  orderby p.LastName
						  select p;
			
			foreach (var item in result3){
				Console.WriteLine("{0}, {1}: {2}", item.LastName, item.FirstName, item.Age);
			}

			Console.WriteLine("\n------");

			var result4 = from p in people
						  orderby p.LastName descending
						  group p by p.LastName;
			
			foreach (var item in result4){
				
				Console.WriteLine(item.Key + ": " + item.Count());
				foreach (var p in item){
					Console.WriteLine("\t{0}, {1}", p.LastName, p.FirstName);
				}
				
			}

			Console.WriteLine("\n------");
			
		}
		
		public class Person {
			public string FirstName { get; set; }
			public string LastName { get; set; }
			public int Age { get; set; }
		}
		
	}
		
}
Output:

AGCRM-MacBook-Pro:Examples user$ dnx . me
Main()
Compiled on DNX451: .NET Framework

LINQExample()
T-h-i-s- -i-s- -a- -t-e-x-t- -e-x-a-m-p-l-e- -t-o- -s-h-o-w- -h-o-w- -L-I-N-Q- -w-o-r-k-s-
------
i-i-a-e-e-a-e-o-o-o-o-
------
i-i-a-e-e-a-e-o-o-o-i-o-
------
a-a-e-e-e-i-i-i-o-o-o-o-
------
o-o-o-o-i-i-i-e-e-e-a-a-
------
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]
System.Linq.Grouping`2[System.Char,System.Char]

------
i-a-e-o-
------
o-i-e-a-
------
a:2
e:3
i:3
o:4

------
Alex Carlstein: 33
Diego Carlstein: 32
Alex Veron: 40
Vanina Veron: 34
Bob Smith: 12

------
Alex Carlstein: 33
Diego Carlstein: 32
Vanina Veron: 34
Bob Smith: 12

------
Alex Carlstein: 33
Diego Carlstein: 32

------
Carlstein, Alex: 33
Carlstein, Diego: 32
Smith, Bob: 12
Veron, Alex: 40
Veron, Vanina: 34

------
Veron: 2
	Veron, Alex
	Veron, Vanina
Smith: 1
	Smith, Bob
Carlstein: 2
	Carlstein, Alex
	Carlstein, Diego

------
Cite this article as: Alejandro G. Carlstein Ramos Mejia, "DNX: C#: LINQ," in Alejandro G. Carlstein Ramos Mejia Blog, September 1, 2015, http://blog.acarlstein.com/?p=3178.

© 2015, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette