Linq extensions examplified

This commit is contained in:
2019-01-06 14:15:40 +01:00
parent 786b398e26
commit ba2e475f5b
7 changed files with 222 additions and 0 deletions

6
Linq_examples/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

8
Linq_examples/Book.cs Normal file
View File

@ -0,0 +1,8 @@
namespace Linq_examples
{
public class Book
{
public string Title { get; set; }
public float Price { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace Linq_examples
{
public class BookRepository
{
public IEnumerable<Book> GetBooks()
{
return new List<Book>
{
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}
};
}
}
}

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{668F191E-426E-498F-A896-A9E5215CD83E}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Linq_examples</RootNamespace>
<AssemblyName>Linq_examples</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Book.cs" />
<Compile Include="BookRepository.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

92
Linq_examples/Program.cs Normal file
View File

@ -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<Book>();
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);
}
}
}

View File

@ -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")]

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VideoEnc", "VideoEnc\VideoE
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionMethods", "ExtensionMethods\ExtensionMethods.csproj", "{7EAB276E-2494-4BA6-BB91-2ADFD22FE331}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionMethods", "ExtensionMethods\ExtensionMethods.csproj", "{7EAB276E-2494-4BA6-BB91-2ADFD22FE331}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Linq_examples", "Linq_examples\Linq_examples.csproj", "{668F191E-426E-498F-A896-A9E5215CD83E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{7EAB276E-2494-4BA6-BB91-2ADFD22FE331}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE