diff --git a/Linq_examples/App.config b/Linq_examples/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/Linq_examples/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Linq_examples/Book.cs b/Linq_examples/Book.cs new file mode 100644 index 0000000..195b191 --- /dev/null +++ b/Linq_examples/Book.cs @@ -0,0 +1,8 @@ +namespace Linq_examples +{ + public class Book + { + public string Title { get; set; } + public float Price { get; set; } + } +} diff --git a/Linq_examples/BookRepository.cs b/Linq_examples/BookRepository.cs new file mode 100644 index 0000000..3936f0a --- /dev/null +++ b/Linq_examples/BookRepository.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace Linq_examples +{ + public class BookRepository + { + public IEnumerable GetBooks() + { + return new List + { + new Book(){Title="ADO.Net Step by step", Price=5}, + new Book(){Title="ASP.Net MVC", Price=9.99f}, + new Book(){Title="ASP.Net Web API", Price=12}, + new Book(){Title="C# Advanced topics", Price=7}, + new Book(){Title="C# Advanced topics", Price=9} + }; + } + } +} diff --git a/Linq_examples/Linq_examples.csproj b/Linq_examples/Linq_examples.csproj new file mode 100644 index 0000000..6fada7f --- /dev/null +++ b/Linq_examples/Linq_examples.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {668F191E-426E-498F-A896-A9E5215CD83E} + Exe + Linq_examples + Linq_examples + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Linq_examples/Program.cs b/Linq_examples/Program.cs new file mode 100644 index 0000000..4a349b5 --- /dev/null +++ b/Linq_examples/Program.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Linq_examples +{ + class Program + { + static void Main(string[] args) + { + var books = new BookRepository().GetBooks(); + + //Books with price less than 10, method 1 + var cheapBooks = new List(); + foreach (var book in books) + { + if (book.Price < 10) + cheapBooks.Add(book); + } + foreach (var book in cheapBooks) + Console.WriteLine(book.Title + " " + book.Price); + //-------------------------------------------------------- + + //Books with price less than 10, method 2 + var cheapBooks2 = books.Where(b => b.Price < 10); + foreach (var book in cheapBooks2) + Console.WriteLine(book.Title + " " + book.Price); + + var cheapBooks3 = books.Where(b => b.Price < 10).OrderBy(b => b.Title); + foreach (var book in cheapBooks3) + Console.WriteLine(book.Title + " " + book.Price); + + //LINQ Query Operators + var cheaperBooks = + from b in books + where b.Price < 10 + orderby b.Title + select b.Title; + + foreach (var book in cheaperBooks) + Console.WriteLine(book); + //-------------------------------------------------------- + + //LINQ Extension Methods + var cheapBooks4 = books + .Where(b => b.Price < 10) + .OrderBy(b => b + .Title).Select(b => b.Title); + + foreach (var book in cheapBooks4) + Console.WriteLine(book); + + //-------------------------------------------------------- + + var sngbook = books.Single(b => b.Title == "ASP.Net MVC"); + Console.WriteLine(sngbook.Title); + + var sngnbook = books.SingleOrDefault(b => b.Title == "ASP.Net MVC++"); + Console.WriteLine(sngnbook==null); + + var fbook = books.First(b => b.Title == "C# Advanced topics"); + Console.WriteLine(fbook.Title+" "+fbook.Price); + + var fdbook = books.FirstOrDefault(b => b.Title == "C# Advanced topicsx"); + Console.WriteLine(fdbook==null); + + var lbook = books.Last(b => b.Title == "C# Advanced topics"); + Console.WriteLine(lbook.Title + " " + lbook.Price); + + var ldbook = books.LastOrDefault(b => b.Title == "C# Advanced topicsx"); + Console.WriteLine(ldbook == null); + + var skiptakebooks = books.Skip(2).Take(3); + foreach (var book in skiptakebooks) + Console.WriteLine(book.Title + " " + book.Price); + + var count = books.Count(); + Console.WriteLine(count); + + var maxPrice = books.Max(b => b.Price); + Console.WriteLine(maxPrice); + + var minPrice = books.Min(b => b.Price); + Console.WriteLine(minPrice); + + var priceSum = books.Sum(b => b.Price); + Console.WriteLine(priceSum); + } + } +} diff --git a/Linq_examples/Properties/AssemblyInfo.cs b/Linq_examples/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3f99d2f --- /dev/null +++ b/Linq_examples/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Linq_examples")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Linq_examples")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("668f191e-426e-498f-a896-a9e5215cd83e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TrainingAdvancedTechniques.sln b/TrainingAdvancedTechniques.sln index e46beab..d70abe8 100644 --- a/TrainingAdvancedTechniques.sln +++ b/TrainingAdvancedTechniques.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VideoEnc", "VideoEnc\VideoE EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionMethods", "ExtensionMethods\ExtensionMethods.csproj", "{7EAB276E-2494-4BA6-BB91-2ADFD22FE331}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Linq_examples", "Linq_examples\Linq_examples.csproj", "{668F191E-426E-498F-A896-A9E5215CD83E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {7EAB276E-2494-4BA6-BB91-2ADFD22FE331}.Debug|Any CPU.Build.0 = Debug|Any CPU {7EAB276E-2494-4BA6-BB91-2ADFD22FE331}.Release|Any CPU.ActiveCfg = Release|Any CPU {7EAB276E-2494-4BA6-BB91-2ADFD22FE331}.Release|Any CPU.Build.0 = Release|Any CPU + {668F191E-426E-498F-A896-A9E5215CD83E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {668F191E-426E-498F-A896-A9E5215CD83E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {668F191E-426E-498F-A896-A9E5215CD83E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {668F191E-426E-498F-A896-A9E5215CD83E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE