Hey all,
I am relatively new to C# and I'm finished with a project, however I am getting two errors that, for the life of me, I cannot figure out why they are coming up. I am doing some queries from the array I created and I am writing the code exactly the same way for each query- receiving exactly what I want. However, (if you look down on the code), my query for "filteredByPrice" is not working.
Thoughts?
using System;
using System.Linq;
using System.Collections.Generic;
public class Invoice
{
//declaring variables
public int quantityValue {get; set;}
public decimal priceValue { get; set; }
// Creates auto-implemented property for PartNumber
public int PartNumber { get; set; }
// Creates auto-implemented property for PartDescription here
public string PartDescription { get; set; }
// Defines the "get" and "set" operators for the Quantity property
// to prevent negative values
private int Quantity
{
get
{
return quantityValue;
}// end get
set
{
if ( value >= 0 )
quantityValue = value;
} // end set
} // end property Quantity
private decimal Price
{
get
{
return priceValue;
}//end get
set
{
if ( value >= 0 )
priceValue = value;
}//end set
} // end property Price
// Class constructor
public Invoice(int number, string description, int quantity, decimal price)
{
quantityValue = quantity;
priceValue = price;
PartNumber = number;
PartDescription = description;
}
}
// end class Invoice
//************************************************************
// The class below creates an array of invoice objects and
// uses LINQ queries to extract specific data from that array
public class LINQInvoiceArray
{
public static void Main(string[] args)
{
//PartNumber Array
Invoice[] Array = new Invoice[8];
Array[0] = new Invoice(83, "Electric sander", 7, 57.98M);
Array[1] = new Invoice(24, "Power saw", 18, 99.99M);
Array[2] = new Invoice(7, "Sledge hammer", 11, 21.50M);
Array[3] = new Invoice(77, "Hammer", 76, 11.99M);
Array[4] = new Invoice(39, "Lawn mower", 3, 79.50M);
Array[5] = new Invoice(68, "Screwdriver", 106, 6.99M);
Array[6] = new Invoice(56, "Jig saw", 21, 11.00M);
Array[7] = new Invoice(3, "Wrench", 34, 7.50M);
// Description query
var sortedByDescription =
from e in Array
orderby e.PartDescription
select new { e.PartDescription};
// display invoices, sorted by description
Display(sortedByDescription, "Sorted by description");
//Sorts items by price
var sortedByPrice =
from a in Array
where a.priceValue >=0
orderby a.priceValue
select new {a.PartNumber, a.quantityValue, a.priceValue};
//Displays price query
Display(sortedByPrice, "Sorted by price");
//Filters items by price
var filteredByPrice =
from b in Array
where b.sortedByPrice >= 10
orderby b.priceValue <= 20
select new { b.PartNumber, b.quantityPrice, b.priceValue };
//Displays query
Display(filteredByPrice, "Filtered by Price");
//Additional Code TBD
} // end Main
//Display method
public static void Display<T>(IEnumerable<T> data, string header)
{
Console.WriteLine("{0}:", header); // display header
// display each item on the console
foreach (var item in data)
Console.WriteLine(item);
Console.WriteLine();
} // end method Display
} // end class LINQInvoiceArray