linq cheat sheet

Solutions on MaxInterview for linq cheat sheet by the best coders in the world

showing results for - "linq cheat sheet"
Thiago
09 Apr 2018
1Dim query = from m in Models
2		join p in PartNumbers on m.ModelId equals p.ModelId
3		where m.Name.Contains("Domane")
4		select new with {.ModelId = m.ModelId, .ModelName = m.Name, .PartNumber = p.InventoryPartNumber }
5
6Dim lambda = Models.Join(PartNumbers, function(m) m.ModelId, function(p) p.ModelId, function(m,p) new with {.m = m, .p = p} ) _
7					.Where( function(j) j.m.Name.Contains("Domane")) _
8					.Select(function(j) new with {.ModelId = j.m.ModelId, .ModelName = j.m.Name, .PartNumber = j.p.InventoryPartNumber})
9
Mads
24 Jan 2018
1	// Single() will throw an error if more than one result, or there is no result
2	var lambda = Statuses.Single( s => s.Name == "Active");
3
4	// SingleorDefault() will throw an error if more than one result, and null if there is no result
5	var lambda = Statuses.SingleOrDefault( s => s.Name == "Active");
6
7	// first() will throw an error if no results
8	var lambda = PartNumbers.First( p => p.ModelId == 10001);
9
10	//firstOrDefault will return null if no results
11	var lambda = PartNumbers.FirstOrDefault( p => p.ModelId == 10001);
12	var lambda = PartNumbers.FirstOrDefault();
13
14
Camron
08 Jan 2018
1Dim query = from m in Manufacturers
2	where m.Name = "Trek" 
3	select new with {.Name = m.Name, .ManufacturerId = m.ManufacturerId}
4
5Dim lambda = Manufacturers.Where(function(m) m.Name = "Trek").Select(function(m) new with {.Name = m.Name, .ManufacturerId = m.ManufacturerId})
6
7
Frieda
15 Nov 2018
1var query = from m in Manufacturers
2	where m.Name == "Trek" 
3	select new {Name = m.Name, Key = m.ManufacturerId};
4
5var lambda = Manufacturers.Where(m => m.Name == "Trek").Select(m=> new {Name = m.Name, Key = m.ManufacturerId});
6
Ilaria
25 Jan 2020
1var query = from m in Manufacturers
2	where m.Name == "Trek" 
3	select m;
4
5var lambda = Manufacturers.Where(m => m.Name == "Trek").Select(m => m);
6
7
Ugo
30 Feb 2018
1	'Single() will throw an error if more than one result, or there is no result
2	Dim lambda = Statuses.Single( function(s) s.Name = "Active")
3	
4	'SingleorDefault() will throw an error if more than one result, and null if there is no result
5	Dim lambda = Statuses.SingleOrDefault( function(s) s.Name = "Active")
6
7	'first() will throw an error if no results
8	Dim lambda = PartNumbers.First( function(p) p.ModelId = 10001)
9	
10	'firstOrDefault will return null if no results
11	Dim lambda = PartNumbers.FirstOrDefault( function(p) p.ModelId = 10001)
12	
13	Dim lambda = PartNumbers.FirstOrDefault()
14
Mat
11 Aug 2017
1var query = from m in Models
2		join p in PartNumbers on m.ModelId equals p.ModelId
3		where m.Name.Contains("Domane")
4		select new {m.ModelId, ModelName = m.Name, PartNumber = p.InventoryPartNumber };
5
6var lambda = Models.Join(PartNumbers, m => m.ModelId, p => p.ModelId, (m, p) => new {m = m, p = p} )
7					.Where( j => j.m.Name.Contains("Domane"))
8					.Select(j => new {ModelId = j.m.ModelId, ModelName = j.m.Name, PartNumber = j.p.InventoryPartNumber});
9
Arianna
28 Mar 2020
1Dim query = from m in Manufacturers
2	where m.Name = "Trek" 
3	select m
4
5Dim lambda = Manufacturers.Where(function(m) m.Name = "Trek").Select(function(m) m)
6