Linq和C#上的复杂语法

时间:2016-03-06 02:08:32

标签: c# xml linq xsd

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

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>

2 个答案:

答案 0 :(得分:0)

所以这并没有完全解释发生了什么,而是打破了Linq:

this

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

AuthorReference

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

Select(..)

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

auth => auth.@ref

大多数Linq查询都遵循variableName => logic的格式。所以你可以做,例如,Students.Where(aStudent => aStudent.Age > 10),并让所有超过10岁的学生。“aStudent”或“auth”都是你想要的。

然后代码继续引用一个名为“@ref”的属性,它具有一些值。所以Select()方法是从所有AuthorReference项中选择@ref的值。

.ToArray();

...只需将其从默认值IEnumerable<T>转换为数组。

最后,Linq浏览了AuthorReference字段/属性,并为您提供了一个@ref值数组。

答案 1 :(得分:0)

当我尝试实现get()

时,我还遇到了另外一个问题
 public Person[] Author
    {
        get
        { return this.Author.Select(a => a.Name).ToArray(); }

我有这个comunicat: “不能隐式地将类型'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;
        }
    }
}