4 Replies - 3723 Views - Last Post: 25 October 2011 - 09:32 PM Rate Topic: -----

#1 ondawall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-January 11

LINQ: Creating Objects of Annonymous Types

Posted 25 October 2011 - 01:36 PM

I am in the process of learning LINQ to Objects. I am querying an array of reference elements using LINQ. In the code below, which I have marked with asteriks and PROBLEM, I am having difficulty returning any values for and I suspect its because of the multiple Select arguments. Can someone shed some light on this for me?

Thanks in advance


'Program name:			Querying an Array of Invoice Objects
'Created/revised:		
'Project description:	To demonstrate the use of LINQ to query objects
'                       instantiated from the Invoice.vb class

Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    Private Sub MainForm_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'initialize array of invoice objects using items listed in Fig 11.8 as reference
        Dim invoices() As Invoice = {
            New Invoice("83", "Electric Sander", 7, 57.98D),
            New Invoice("24", "Power Saw", 18, 99.9D),
            New Invoice("7", "Sledge Hammer", 11, 21.5D),
            New Invoice("77", "Hammer", 76, 11.99D),
            New Invoice("39", "Lawn Mower", 3, 79.5D),
            New Invoice("68", "Screwdriver", 106, 6.99D),
            New Invoice("56", "Jig Saw", 21, 11D),
            New Invoice("3", "Wrench", 34, 7.5D)}

        'display items in original array
        outputTextBox.AppendText(String.Format("Original array:{0}", vbCrLf))
        For Each partinvoice As Invoice In invoices
            outputTextBox.AppendText(String.Format("{0}{1}", partinvoice, vbCrLf))
        Next

        'OK-Sort invoice objects by PartDescription and display result in form textbox
        Dim partLine As IEnumerable =
            From line In invoices
            Order By line.PartDescription
            Select line

        outputTextBox.AppendText(String.Format("{0}Sorted Invoice objects by Part Description:{1}", vbCrLf, vbCrLf))
        For Each part As Invoice In partLine
            outputTextBox.AppendText(String.Format("  {0}{1}", part, vbCrLf))
        Next

        'OK-sort invoice objects by price and display invoices sorted by price
        Dim priceSorted As IEnumerable =
            From line In invoices
            Order By line.Price
            Select line

        outputTextBox.AppendText(String.Format("{0}Sorted Invoice objects by Price:{1}", vbCrLf, vbCrLf))
        For Each part As Invoice In priceSorted
            outputTextBox.AppendText(String.Format("  {0}{1}", part, vbCrLf))
        Next
  

******PROBLEM***********
Select the PartDescription and Quantity and sort results by Quantity
sort invoice objects by price and display invoices sorted by price
This is not working. The partAndQuantity variable is not declared properly.
Option Strict On is rightfully forcing me to declare the variable as a type
What type is Annonymous or is there such a thing? I dont see that there is.
Is it possible to assign an AS Annonymous type? Ex. Dim partAndQuantity As ???????
This LINQ creates an annonymous class due to the multiple field Select arguments
]
        Dim partAndQuantity As IEnumerable =
           From line In invoices
           Order By line.Quantity
           Select line.PartDescription, line.Quantity  '<---- creates annonymous class type

        'displays partAndQuantity in textbox
        outputTextBox.AppendText(String.Format("{0}Description and Quantity, sorted by Quantity:{1}", vbCrLf, vbCrLf))
        For Each part As Invoice In partAndQuantity
            'outputTextBox.AppendText(partAndQuantity.ToString)
            outputTextBox.AppendText(String.Format("  {0}{1}", part, vbCrLf))
        Next

    End Sub
End Class

This post has been edited by AdamSpeight2008: 25 October 2011 - 01:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: LINQ: Creating Objects of Annonymous Types

#2 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: LINQ: Creating Objects of Annonymous Types

Posted 25 October 2011 - 01:49 PM

Don't specify the type as it can be inferred.
Anonymous types only have local scoping, so if you want to extend there life - create a class and store this information in it.
Was This Post Helpful? 1
  • +
  • -

#3 ondawall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-January 11

Re: LINQ: Creating Objects of Annonymous Types

Posted 25 October 2011 - 03:38 PM

Thanks for your response.

When I enter the code below not specifying a type, the variable is blue-lined with the following message:

"Option Strict On requires all variable declarations to have an 'As' clause"



Dim partAndQuantity =
        From line In invoices
        Order By line.Quantity
        Select line.PartDescription, line.Quantity

        outputTextBox.AppendText(String.Format("{0}Description and Quantity, sorted by Quantity:{1}", vbCrLf, vbCrLf))
        For Each part As Invoice In partAndQuantity
            'outputTextBox.AppendText(partAndQuantity.ToString)
            outputTextBox.AppendText(String.Format("  {0}{1}", part, vbCrLf))

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: LINQ: Creating Objects of Annonymous Types

Posted 25 October 2011 - 05:13 PM

You have to set Option Infer On.

This post has been edited by AdamSpeight2008: 25 October 2011 - 05:13 PM

Was This Post Helpful? 1
  • +
  • -

#5 ondawall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-January 11

Re: LINQ: Creating Objects of Annonymous Types

Posted 25 October 2011 - 09:32 PM

That worked famously. Many thanks Adam.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1