Hello, I'm needing help with a query I've been working on for a week now, I've tried using IEnumerable and tried using the query like a var, to no avail. Basically I'm just trying to query a range using LINQ for example, I need to find the range of invoices between $250 - $550 in descQuantity. My query is close to the bottom called rangeQuery. Thank you. Also, a strange exception keeps telling me to look at lines 104 and 107...I don't have a line 107 and 104 is a bracket at the end(2nd to last)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class LINQWithArrayOfObjects
{
public static void Main(string[] args)
{
// initialize array of invoices
Invoice[] theInvoice = {
new Invoice( 83, "Electric sander", 7,57.98M ),
new Invoice( 24, "Power saw", 18, 99.99M ),
new Invoice( 7, "Sledge hammer", 11, 21.50M),
new Invoice( 77, "Hammer", 76, 11.99M),
new Invoice( 39, "Lawn mower", 3, 79.50M),
new Invoice( 68, "Screwdriver", 106, 6.99M ),
new Invoice( 56, "Jig saw", 21, 11.00M),
new Invoice( 3, "Wrench", 34, 7.50M )
};
//Sorted by part Description
var sortedByDesc =
from inv in theInvoice
orderby inv.PartDescription
select inv;
Console.WriteLine("by part Description");
Console.WriteLine("==========================================");
// dispay by part Description
foreach (var inv in sortedByDesc)
{
Console.WriteLine(inv.ToString());
}
Console.WriteLine("==========================================");
// Sorted by price
var sortedByPrice =
from priceInvoice in theInvoice
orderby priceInvoice.Price
select priceInvoice;
// display by price
Console.WriteLine("by part Price");
Console.WriteLine("==========================================");
foreach (var priceInvoice in sortedByPrice)
{
Console.WriteLine(priceInvoice.ToString());
}
Console.WriteLine("==========================================");
// sort by quantity
var partQuantity =
from inv in theInvoice
orderby inv.Quantity
select new { inv.PartDescription, inv.Quantity };
//display by quantity
Console.WriteLine("by part Quantity");
Console.WriteLine("==========================================");
foreach (var inv in partQuantity)
{
Console.WriteLine(inv.ToString());
}
Console.WriteLine("==========================================");
// Invoice Total
var descQuantity =
from inv in theInvoice
let InvoiceTotal = inv.Price * inv.Quantity
orderby InvoiceTotal
select new { InvoiceTotal, inv.PartDescription };
Console.WriteLine("Invoice Total");
Console.WriteLine("==========================================");
// display Invoice Totals
foreach (var inv in descQuantity)
Console.WriteLine(inv.ToString());
Console.WriteLine("==========================================");
Console.WriteLine("Range of $200 - $500");
Console.WriteLine("==========================================");
var rangeQuery =
from InvoiceTotal in descQuantity
where Convert.ToDecimal(InvoiceTotal) >= 200 && Convert.ToDecimal (InvoiceTotal) <= 500
select InvoiceTotal;
foreach (var InvoiceTotal in rangeQuery)
{
Console.WriteLine(InvoiceTotal);
}
}