我是 vb.net 和 linq-to-xml 的新手。我正在尝试将常用查询封装到函数中,以便将它们链接起来。
From tag in myelement where ... select ...
上面,myelement 可以是 IEnumerable(Of XElement) 或 XElement,Visual Studio 对此没有任何问题。但是如果我定义一个函数
Function select_items(ByRef myelement As IEnumerable(Of XElement), ByVal name As String) As IEnumerable(Of XElement)
Dim tags = _
From tag In myelement.Elements(ns + "item")
Where tag.Element(ns + "cc").Attribute("S") = "ITEM" _
AndAlso tag.Element(ns + "cc").Value = name
Select tag
Return tags
End Function
如果我没有为 myelement 输入类型,则会出现错误
Expression of type 'Object' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.
如果我将“myelement As Xelement”放在函数声明中,那么我在参数中使用 IEnumerable(of XElement) 调用该函数,它会显示错误
Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.Xml.Linq.XElement]' to type 'System.Xml.Linq.XElement'.
如果我输入“myelement As IEnumerable(of Xelement)”并用 XElement 调用它,我会得到相同的错误反转
所以问题是:我应该如何声明我的函数具有与 from/in/where/select 语句相同的多态性?
谢谢