【问题标题】:Linq syntax in VB.NETVB.NET 中的 Linq 语法
【发布时间】:2009-05-28 09:42:21
【问题描述】:

我真正想要的是像 Scott Gu 的博客中那样将这两个表选择为匿名类型:here 但是,我会满足于这种创建的类型“ActiveLots”我将两个表连接在一起并希望能够从我的结果集中引用每个列。

我似乎没有正确理解语法。

 Dim pi = From p In dc.Inventories Join i In dc.InventoryItems On p.InventoryItemID _
                     Equals i.InventoryItemID Where p.LotNumber <> "" _
                     Select New ActiveLots LotNumber = p.LotNumber, Quantity = p.Quantity, Item = i.Item, Uom = i.UnitofMeasure, Description = i.Description

【问题讨论】:

    标签: vb.net linq anonymous-types


    【解决方案1】:

    看看Daniel Moth's blog entry。我怀疑你想要:

    Dim pi = From p In dc.Inventories _
             Join i In dc.InventoryItems
                  On p.InventoryItemID Equals i.InventoryItemID _
             Where p.LotNumber <> "" _
             Select New With { .LotNumber = p.LotNumber, .Quantity = p.Quantity, _
                               .Item = i.Item, .Uom = i.UnitofMeasure, _
                               .Description = i.Description }
    

    这是使用匿名类型 - 要使用具体类型,您将使用 New ActiveLots With { ...(其中 ActiveLots 必须有一个无参数构造函数)。

    【讨论】:

    • 乔恩,你关于“新的 ActiveLot ...”的假设是正确的。显然 ActiveLot 需要一个空的构造函数。
    • 感谢 Meta-Knight 的确认。将进行适当的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多