我有一个实现IEnumerable但没有实现的类IEnumerable<T>.我不能改变这个类,我不能使用另一个类而不是它.正如我从MSDN中所理解的那样,如果类实现,可以使用LINQIEnumerable<T>.我尝试过使用instance.ToQueryable(),但它仍然没有启用LINQ方法.我确信这个类只能包含一种类型的实例,所以类可以实现IEnumerable<T>,但它没有.那么我该怎么做才能使用LINQ表达式查询这个类?
我以为我在回答这个问题时看到了一个错误,并指出了这一点.我被告知我不正确,我的答案后来被删除了.
我仍然不知道自己错了.因此,我在这里发帖,希望有人能解释我对我的误解.
我回答的答案解释了apply-templates的使用.它包含以下XML和XSL,描述了模板的匹配方式:
<!-- sample XML snippet -->
<xml>
<foo /><bar /><baz />
</xml>
<!-- sample XSLT snippet -->
<xsl:template match="xml">
<xsl:apply-templates select="*" /> <!-- three nodes selected here -->
</xsl:template>
<xsl:template match="foo"> <!-- will be called once -->
<xsl:text>foo element encountered</xsl:text>
</xsl:template>
<xsl:template match="xml/*"> <!-- will be called twice -->
<xsl:text>other element countered</xsl:text>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
我的评论是最后一个模板应该是:
<xsl:template match="*"> <!-- will be called twice -->
<xsl:text>other element countered</xsl:text>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
因为当前节点已经存在 <xml>
有人告诉我:
不,xml/*是一种模式,它匹配名称为xml的元素的子元素.
测试原始答案
但是,使用这个XML:
<xml>
<foo /><bar /><baz …Run Code Online (Sandbox Code Playgroud) 我正在尝试序列化Outer下面显示的类,并XElement从序列化的XML 创建一个.它有一个类型的财产Inner.我想更改Inner(to Inner_X)和Outer(to Outer_X)的名称.
class Program
{
static void Main(string[] args)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(Outer));
xmlSerializer.Serialize(streamWriter, new Outer());
XElement result = XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
}
}
}
}
[XmlType("Outer_X")]
public class Outer
{
public Outer()
{
this.InnerItem = new Inner();
}
public Inner InnerItem { get; set; }
}
[XmlType("Inner_X")]
public class Inner
{ …Run Code Online (Sandbox Code Playgroud)