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

Recommended Answers

All 4 Replies

in the "Array" there is no field called "sortedByPrice"

var filteredByPrice = from b in Array     
where [B]b.sortedByPrice[/B] >= 10     
orderby b.priceValue <= 20 //also, I'm not sure if this will work    
select new { b.PartNumber, b.quantityPrice, b.priceValue };

Ionut

orderby b.priceValue <= 20 is wrong.

in the "Array" there is no field called "sortedByPrice"

var filteredByPrice = from b in Array     
where [B]b.sortedByPrice[/B] >= 10     
orderby b.priceValue <= 20 //also, I'm not sure if this will work    
select new { b.PartNumber, b.quantityPrice, b.priceValue };

Ionut

That's a valid point and is there any reason why you create an anymous class(using var)

var sortedByPrice =
from a in Array
where a.priceValue >=0
orderby a.priceValue
select new {a.PartNumber, a.quantityValue, a.priceValue};

In this situation, since all returned values are properties of Invoice, you coud simply do

IEnumerable<Invoice> sortedByPrice =
from a in Array
where a.priceValue >=0
orderby a.priceValue
select a;

That's a valid point and is there any reason why you create an anymous class(using var)

var sortedByPrice =
from a in Array
where a.priceValue >=0
orderby a.priceValue
select new {a.PartNumber, a.quantityValue, a.priceValue};

In this situation, since all returned values are properties of Invoice, you coud simply do

IEnumerable<Invoice> sortedByPrice =
from a in Array
where a.priceValue >=0
orderby a.priceValue
select a;

Anonymous types are perfectly fine.

Besides, this could just be stub code-- he could have an even bigger class he plans to do this with, and the LINQ could then be used as a translation layer. Cherry-picking individual properties from an object would allow you to send a smaller object across a line connecting to a web service, too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.