4 Replies - 1415 Views - Last Post: 10 April 2015 - 05:50 AM Rate Topic: -----

#1 waketech123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-November 14

issue with using results of previous LINQ to filter by range

Posted 09 April 2015 - 11:40 AM

Hey I am working on a projet and I am almost done but I am stuck on part E, here are the 5 parts

  • Use LINQ to sort the Invoice objects by PartDescription.
  • Use LINQ to sort the Invoice objects by UnitPrice.
  • Use LINQ to select the PartDescription and Quantity and sort the results by Quantity.
  • Use LINQ to select from each invoice the PartDescription and the invoice total amount. The GetInvoiceAmount method returns the total amount of the invoice. Order the results by invoice total amount. [Hint: Use let in the LINQ query to store invoice total amount in a new variable. See the example in textbook Figure 9.7]
  • Using the results of the LINQ query in part (d), select invoices with total amount in the range $200 to $500.


here is Invoice.cs
// Invoice.cs
// Invoice for a hardware company

using System;

public class Invoice
{
    string partNum;
    string partDescription;
    int quantity;
    decimal unitPrice;

    public Invoice(string pNum, string pDesciption, int q, decimal uPrice)
    {
        PartNum = pNum;
        PartDescription = pDesciption;
        Quantity = q;
        UnitPrice = uPrice;
    }

    public string PartNum
    {
        get
        {
            return partNum;
        } // end get
        set
        {
            partNum = value;
        } // end set
    } // end 

    public string PartDescription
    {
        get
        {
            return partDescription;
        } // end get
        set
        {
            partDescription = value;
        } // end set
    } // end 

    public int Quantity
    {
        get
        {
            return quantity;
        } // end get
        set
        {
            if (value >= 0)
                quantity = value;
        } // end set
    } // end 

public decimal UnitPrice
    {
        get
        {
            return unitPrice;
        } // end get
        set
        {
            if (value >= 0M)
                unitPrice = value;
        } // end set
    } // end 

    public decimal GetInvoiceAmount()
    {
        decimal amt = Quantity * UnitPrice;
        return amt;
    }

    public override string ToString()
    {
        return string.Format("{0,-10}{1,-20}{2,-10}{3,10:C}", PartNum, 
   PartDescription, Quantity, UnitPrice);
    }
}


and the main method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lab12p1
{
    class Program
    {
        static void Main(string[] args)
        {
            Invoice[] Invoices = {
            new Invoice ("83", "Electric sandar", 7, 57.98m),
            new Invoice ("24", "Power Saw", 18, 99.99m),
            new Invoice ("7", "Sledge Hammer", 11, 21.5m),
            new Invoice ("77", "Hammer", 76, 11.99m),
            new Invoice ("39", "Lawn mower", 3, 79.5m),
            new Invoice("68", "Screwdriver", 106, 6.99m),
            new Invoice("56", "Jig saw", 21, 11.00m),
            new Invoice("3", "Wrench", 34, 7.5m) 
                                 
                                 };

            //display all of invoices

            Console.WriteLine("Orignal array:");
            Console.WriteLine("\nPartnum  partdescript        Quantity    Unit Price \n");
            foreach (var elemnt in Invoices)
                Console.WriteLine(elemnt);

            //order invoices by invoice number string

            var descriptSorted =
                from Invoice in Invoices
               orderby Invoice.PartDescription
                select Invoice;

            Console.WriteLine("\nInvoices sorted by part description ");
            foreach (var element in descriptSorted)
                Console.WriteLine(element);

          //sort by unit price
            var unitpriceSorted =
               from Invoice in Invoices
               orderby Invoice.UnitPrice
               select Invoice;

            Console.WriteLine("\nInvoices sorted by unit price ");

            foreach (var element in unitpriceSorted)
                Console.WriteLine(element);

            //select description and quanity then sort them
            var names =
                from Invoice in Invoices
                orderby Invoice.Quantity
                select new { Invoice.PartDescription, Invoice.Quantity }
               ;

            Console.WriteLine("\nPart description and quantity sorted by quantity: ");

            foreach (var element in names)
                Console.WriteLine(element);
            //select description and totalamount and order by total amount
            var totalAmount =
                from Invoice in Invoices
                let totalamount = Invoice.GetInvoiceAmount()
                orderby totalamount
                select new { Invoice.PartDescription, totalamount };

            Console.WriteLine("\nPart description and invoice total sorted by invoice total");
            foreach (var elementt in totalAmount)
                Console.WriteLine(elementt);

            //select results that have a range of 200 to 500
            var range =
                from Invoice in Invoices
                let totalamountt = Invoice.GetInvoiceAmount()
                where totalamountt <= 500 && totalamountt >= 200
                orderby totalamountt
                select new { Invoice.PartDescription, totalamountt };

            Console.WriteLine("\nRange for 200-500");
                foreach (var element in range)
                    Console.WriteLine(element);
            
             Console.ReadKey();
        }
       
    }
}



I am confused on the syntax of how to use the LINQ results of totalAmount to then filter for the range later in the program instead of creating another whole query like i did with "var range = " at the end.

Thank you

This post has been edited by tlhIn`toq: 09 April 2015 - 12:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: issue with using results of previous LINQ to filter by range

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 466
  • View blog
  • Posts: 2,212
  • Joined: 07-April 08

Re: issue with using results of previous LINQ to filter by range

Posted 09 April 2015 - 11:46 AM

presumably totalAmount is a List of some sort. You can use the Linq Join keyword to join the two tables together.


Also your sort methods can be simplified by just calling Invoices.OrderBy()


so this:
var unitpriceSorted = from Invoice in Invoices
                      orderby Invoice.UnitPrice
                      select Invoice;

foreach (var element in unitpriceSorted)
    Console.WriteLine(element);




can just be this:

foreach ( var element in Invoices.OrderBy(i => i.UnitPrice) )
{
    Console.WriteLine(element);
}



Also it's good practice to encase your code blocks in {'s even if they are one line.

This post has been edited by rgfirefly24: 09 April 2015 - 11:50 AM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: issue with using results of previous LINQ to filter by range

Posted 09 April 2015 - 12:50 PM

I fixed up your homework instruction list because it was displaying funny.

Tip: Don't try to hand annotate lists with parentheses. The emoticon parser for the site tries to make sense of them and you get:

(a)
(B)
©
(d)
(e)

Instead just enter the lines then encase them with list tags using the List button in tool bar
Was This Post Helpful? 1
  • +
  • -

#4 Psyguy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 365
  • Joined: 12-January 11

Re: issue with using results of previous LINQ to filter by range

Posted 09 April 2015 - 12:57 PM

One piece of advise...separate your code into methods for specific functionality. For example, you could do the following:

public static void Main(string[] args)
{
     var invoices = GetInvoices();
}

public static Invoice[] GetInvoices()
{
     Invoice[] Invoices = {
                            new Invoice ("83", "Electric sandar", 7, 57.98m),
                            new Invoice ("24", "Power Saw", 18, 99.99m),
                            new Invoice ("7", "Sledge Hammer", 11, 21.5m),
                            new Invoice ("77", "Hammer", 76, 11.99m),
                            new Invoice ("39", "Lawn mower", 3, 79.5m),
                            new Invoice("68", "Screwdriver", 106, 6.99m),
                            new Invoice("56", "Jig saw", 21, 11.00m),
                            new Invoice("3", "Wrench", 34, 7.5m)
                          };
     return Invoices;
}



This way, your code is cleaner and reusable if you happen to need the same functionality later.

This post has been edited by Psyguy: 09 April 2015 - 01:01 PM

Was This Post Helpful? 2
  • +
  • -

#5 waketech123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-November 14

Re: issue with using results of previous LINQ to filter by range

Posted 10 April 2015 - 05:50 AM

OK guys thank you for all the help and advice, I appreciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1