【问题标题】:Complicated syntax over Linq and C#Linq 和 C# 上的复杂语法
【发布时间】:2016-03-06 02:08:32
【问题描述】:

我不知道如何理解以下语法:

public string[] AuthorReferenceID
{
    get { return this.AuthorReference.Select(auth => auth.@ref).ToArray(); }
}

这是由 Xsd2 Code Generator 从 XML Schema 生成的文件 Bookstore.cs 的一部分。上面的代码已添加到生成的文件中。已创建整个应用程序以显示 XML 文件的内容。

XSD 文档(没有前 3 行)如下所示:

<xs:complexType name="Person">
        <xs:sequence>
          <xs:element name="Name" type="xs:string" />
          <xs:element name="Surname" type="xs:string" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" />
      </xs:complexType><xs:complexType name="PersonReference">
        <xs:attribute name="ref" type="xs:string" />
      </xs:complexType>

      <xs:complexType name="Publication">
        <xs:sequence>
          <xs:element name="AuthorReference" minOccurs="1" maxOccurs="unbounded" type="b:PersonReference" />
        </xs:sequence>
        <xs:attribute name="title" type="xs:string" use="required" />
      </xs:complexType>

      <xs:complexType name="Book">
        <xs:complexContent>
          <xs:extension base="b:Publication">
            <xs:attribute name="price" type="b:positiveDecimal" use="required" />
            <xs:attribute name="category" type="b:itemCategory" use="required" />
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>

      <xs:simpleType name="positiveDecimal">
        <xs:restriction base="xs:decimal">
          <xs:minExclusive value="0" />
          <xs:fractionDigits value="2" />
        </xs:restriction>
      </xs:simpleType>

      <xs:simpleType name="itemCategory">
        <xs:restriction base="xs:string">
          <xs:enumeration value="ITC" />
          <xs:enumeration value="Mathemath" />
        </xs:restriction>
      </xs:simpleType>

      <xs:element name="Bookstore">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Books">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Book" minOccurs="0" maxOccurs="unbounded" type="b:Book" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="Journals">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Journal" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element maxOccurs="unbounded" name="Article" type="b:Publication" />
                        <xs:element name="EditorReference" minOccurs="1" maxOccurs="unbounded" type="b:PersonReference" />
                      </xs:sequence>
                      <xs:attribute name="price" type="b:positiveDecimal" use="required" />
                      <xs:attribute name="category" type="b:itemCategory" use="required" />
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="People">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Person" type="b:Person" minOccurs="0" maxOccurs="unbounded" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
        <xs:key name="PERSON_ID">
          <xs:selector xpath="b:People/b:Person" />
          <xs:field xpath="@id" />
        </xs:key>
        <xs:keyref name="ATHBK_PERSON_ID" refer="b:PERSON_ID">
          <xs:selector xpath="b:Books/b:Book/b:AuthorReference" />
          <xs:field xpath="@ref" />
        </xs:keyref>
        <xs:keyref name="ATHART_PERSON_ID" refer="b:PERSON_ID">
          <xs:selector xpath="b:Journals/b:Journal/b:Article/b:AuthorReference" />
          <xs:field xpath="@ref" />
        </xs:keyref>
        <xs:keyref name="EDITJRL_PERSON_ID" refer="b:PERSON_ID">
          <xs:selector xpath="b:Journals/b:Journal/b:EditorReference" />
          <xs:field xpath="@ref" />
        </xs:keyref>
      </xs:element>
    </xs:schema>

【问题讨论】:

  • 您能否编辑您的问题以包含属性“AuthorReference”(我假设它是一个类的列表或数组),以及(如果可以的话)AuthorReference 是其集合的类?
  • “不懂”是什么意思?您是否难以理解这应该返回什么、=&gt; 符号或其他什么?这是一个非常简单的 LINQ 调用,因此不清楚您在哪里遇到了问题。

标签: c# xml linq xsd


【解决方案1】:

所以这并不能完全解释发生了什么,而是要分解 Linq:

this

“this”是一个关键字,意思是“这个对象”(或者,换句话说,“这个类的这个实例”)。它不是严格需要的,因为除非另有说明,否则对象假定它们使用自己的方法/属性。

AuthorReference

在“this”类中有一个名为“AuthorReference”的属性或字段。它是如何使用的我可以假设它是某种集合,可能是一个列表或一个数组。但是,如果没有看到它的实现,我无法提供具体细节。

Select(..)

“Select”是一种 Linq 方法,它根据某些逻辑从集合(在本例中为“AuthorReference”)中选择属性或对象。

auth => auth.@ref

大多数 Linq 查询都遵循variableName =&gt; logic 的格式。例如,您可以使用Students.Where(aStudent =&gt; aStudent.Age &gt; 10),并获取所有超过 10 岁的学生。“aStudent”或“auth”都是您希望他们成为的任何人。

然后代码似乎引用了一个名为“@ref”的属性,该属性具有一定的价值。所以 Select() 方法是从所有 AuthorReference 项中选择 @ref 的值。

.ToArray();

...只需将其从默认值IEnumerable&lt;T&gt; 转换为数组即可。

最后,Linq 会通过 AuthorReference 字段/属性,并为您提供一组 @ref 值。

【讨论】:

    【解决方案2】:

    当我尝试实现 get() 时,我还偶然发现了一个问题

     public Person[] Author
        {
            get
            { return this.Author.Select(a => a.Name).ToArray(); }
    

    我有通讯器: "不能将类型 'string[]' 隐式转换为 'Bookstore.Person[]'。

    代码的重要部分如下所示:

    public partial class Publication: IPublication
    {
        public Person[] Author
        {
            get
            { return this.Author.Select(a => a.Name).ToArray(); }
    
    
    
            set
            {
                throw new NotImplementedException();
            }
        }
    
    
        public string[] AuthorReferenceID
        {
            get { return this.AuthorReference.Select(auth => auth.@ref).ToArray(); }
        }
    }
    

    }

    界面如下:

       public interface IPublication
    {
        Person[] Author { get; set; }
        string title { get; set; }
        string[] AuthorReferenceID { get; }
    }
    

    其余代码如下:

     [System.Xml.Serialization.XmlIncludeAttribute(typeof(Book))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://bookstore.com")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://bookstore.com/", IsNullable=true)]
    public partial class Publication {
    
        private PersonReference[] authorReferenceField;
    
        private string titleField;
    
    
        [System.Xml.Serialization.XmlElementAttribute("AuthorReference")]
        public PersonReference[] AuthorReference {
            get {
                return this.authorReferenceField;
            }
            set {
                this.authorReferenceField = value;
            }
        }
    
    
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title {
            get {
                return this.titleField;
            }
            set {
                this.titleField = value;
            }
        }
    }
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://bookstore.com/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://bookstore.com/", IsNullable=true)]
    public partial class PersonReference {
    
        private string refField;
    
        /// <uwagi/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string @ref {
            get {
                return this.refField;
            }
            set {
                this.refField = value;
            }
        }
    }
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://bookstore.com/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://bookstore.com/", IsNullable=true)]
    public partial class Person {
    
        private string nameField;
    
        private string surnameField;
    
        private string idField;
    
    
        public string Name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
    
    
        public string Surname {
            get {
                return this.surnameField;
            }
            set {
                this.surnameField = value;
            }
        }
    
        /// <uwagi/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
    }
    

    【讨论】:

    • 嘿 hegendroffer,我看到这个只是因为我碰巧正在浏览我以前的答案。将来,如果您将其发布为对先前问题的答案,人们将看到您的问题的扩展的可能性微乎其微。如果您对某个主题有几天没有活动的新问题,请继续将其作为新问题发布;这样你会得到更多的关注。不过,关于这个问题。你想达到什么目的?看起来您只是想允许获取“作者”,但永远不会(永远,永远)设置。我相信这应该将其保留为空数组? >>
    • >> 另外,您想要返回一个 Person 对象数组还是一个作者姓名数组?如果你澄清你想要完成什么/你想要什么,我很乐意给你一个更好的答案。照原样,我只是不确定如何指导你。
    • 感谢您的回复!我想得到一个 Person 数组,所以我将能够(通过序列化)在屏幕上打印它们。这是我大学的任务,我有点卡住了,但我会感谢任何帮助:) BTW。谢谢你的提示:)
    猜你喜欢
    • 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
    相关资源
    最近更新 更多