English 中文(简体)
Linq 在VB.net进行匿名和严格选择
原标题:Linq Grouping in VB.net with anonymous types and option strict
  • 时间:2012-04-24 11:34:57
  •  标签:
  • vb.net
  • linq

我敢于做一些事情,这应当简单明了,但与林克和瓦布作斗争。 我下面谈一下,在这里,我把收集工作按适当分类,然后想要获得我的团体的财产。 问题 我有匿名打字。 由于选择严格,我必须给出一个明确的类型,但我可以确定哪一种类型。 The following don t volume because t.HeadAccount 关键数值(因为t有某种物体)。 因此,我要么需要做一些投放,要么我的林克挑选人错了,

public class TempObject
    public HeadKey as string
    Public SubKey as string
    Public SomeValue as decimal
end class

public class GroupTest
public sub RunTest
    dim collection new List(of TempObject) = GetTestCollection()

    Dim groupedValues As IEnumerable = From n In collection _
        Group By key = n.HeadKey Into Group Select HeadKeyValue = key, SubValues = Group

    for each t as object in groupedValues
    debug.print(t.HeadKeyValue)
        next

End sub

private function GetTestCollection() as List(of TempObject)
    dim result as new list(of tempobject)
    Dim x As New tempObject
    x.HeadAccountNumber = "A"
        x.SubAccountNumber = "B"
        x.Value = 500
        result.Add(x)
        x = New tempObject
        x.HeadAccountNumber = "A"
        x.SubAccountNumber = "C"
        x.Value = 600
        result.Add(x)
        x = New tempObject
        x.HeadAccountNumber = "B"
        x.SubAccountNumber = "D"
        x.Value = 100
    result.add(x)
    return result
end function
End class
最佳回答

无需具体说明类型,至少需要Option Infer on(可提议,不得与Option Strict相混淆)。

如下:

Dim collection = GetTestCollection()
Dim groupedValues = From n In collection
    Group n By n.HeadKey Into Group

For Each t In groupedValues
    Dim hk = t.HeadKey
    Dim group = t.Group
Next
问题回答

暂无回答




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签