Convert TotalCharges and TotalFrequentRenterPoints from loop to Linq extensions methods ConvertLoopToLinqSum
authorMichael Welch <michaelgwelch@gmail.com>
Thu, 9 Feb 2012 02:14:24 +0000 (20:14 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Thu, 9 Feb 2012 02:14:24 +0000 (20:14 -0600)
.gitignore [new file with mode: 0644]
RefactoringChapter1.sln
RefactoringChapter1/Customer.cs
RefactoringChapter1/RefactoringChapter1.csproj

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..fa8a8a1
--- /dev/null
@@ -0,0 +1,2 @@
+bin/
+*.userprefs
index 425c9d4..c5617ff 100644 (file)
@@ -1,6 +1,6 @@
 \feff\r
-Microsoft Visual Studio Solution File, Format Version 10.00\r
-# Visual Studio 2008\r
+Microsoft Visual Studio Solution File, Format Version 11.00\r
+# Visual Studio 2010\r
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RefactoringChapter1", "RefactoringChapter1\RefactoringChapter1.csproj", "{45B46538-6022-4C6A-9C27-48E1E5B5EA02}"\r
 EndProject\r
 Global\r
index 16981bd..93cab22 100644 (file)
@@ -1,4 +1,5 @@
 using System;
+using System.Linq;
 using System.Collections.Generic;
 
 namespace RefactoringChapter1
@@ -18,7 +19,8 @@ namespace RefactoringChapter1
                        rentals.Add (rental);
                }
                
-               public string Name {
+               public string Name
+               {
                        get { return _name; }
                }
                
@@ -45,10 +47,10 @@ namespace RefactoringChapter1
                        
                }
                
-               public string HtmlStatement() 
+               public string HtmlStatement ()
                {
                        string result = "<H1>Rentals for <EM>" + Name + "</EM></H1><P>\n";
-                       foreach(Rental each in rentals)
+                       foreach (Rental each in rentals)
                        {
                                result += each.Movie.Title + ": " + each.Charge + "<BR>\n";
                        }
@@ -63,12 +65,9 @@ namespace RefactoringChapter1
                {
                        get
                        {
-                               double result = 0;
-                               foreach(Rental each in rentals)
-                               {
-                                       result += each.Charge;
-                               }
-                               return result;
+                               
+                               return rentals.Sum(rental => rental.Charge);
+
                        }
                }
                
@@ -76,12 +75,7 @@ namespace RefactoringChapter1
                {
                        get
                        {
-                               int result = 0;
-                               foreach(Rental each in rentals)
-                               {
-                                       result += each.FrequentRenterPoints;
-                               }
-                               return result;
+                               return rentals.Sum(rental => rental.FrequentRenterPoints);
                        }
                }
 
index ed72d2e..dd5df9f 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -9,7 +9,6 @@
     <OutputType>Library</OutputType>
     <RootNamespace>RefactoringChapter1</RootNamespace>
     <AssemblyName>RefactoringChapter1</AssemblyName>
-    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -31,6 +30,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Movie.cs" />