代码之家  ›  专栏  ›  技术社区  ›  Cory House

将LINQ转换为XML查询,从C转换为VB.NET。你能找出我的错误吗?

  •  0
  • Cory House  · 技术社区  · 15 年前

    我正在将下面的linq查询从c转换为vb.net。你能找出我的错误吗?该查询联接3个XML数据集。事先谢谢!

    C-这个很好用。

    List<Course> courses =
      (from course in CourseXML.Descendants(ns + "row")
      join coursecategory in CourseCategoryXML.Descendants("Table") on (string)course.Attribute("code") equals (string)coursecategory.Element("DATA")
      join category in CategoryXML.Descendants("Table") on (string)coursecategory.Element("GRP") equals (string)category.Element("GRP")
      where (string)coursecategory.Element("RECTYPE") == "C"
      select new Course {
        CategoryCode = category.Element("GRP").Value,
          Code = course.Attribute("code").Value
      }).ToList<Course>();
    

    我没有从中得到任何结果,所以我怀疑我要么铸造不当,要么加入不当。

    Dim result = (From course In CourseXML.Descendants(ns + "row") _
    Join coursecategory In CourseCategoryXML.Descendants("Table") On CType(course.Attribute("code"), String) Equals CType(coursecategory.Element("DATA"), String) _
    Join category In CategoryXML.Descendants("Table") On CType(coursecategory.Element("GRP"), String) Equals CType(category.Element("GRP"), String) _
    Where CType(coursecategory.Element("RECTYPE"), String) = "C" _
    Select New Course() With _ 
    { _
      .CategoryCode = category.Element("GRP").Value, _
      .Code = course.Attribute("code").Value _
    }).ToList()
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Jim    15 年前

    我使用以下站点进行转换: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Dim courses As List(Of Course) = (From course In CourseXML.Descendants(ns & "row") _
        Join coursecategory In CourseCategoryXML.Descendants("Table") On DirectCast(course.Attribute("code"), String) = DirectCast(coursecategory.Element("DATA"), String) _
        Join category In CategoryXML.Descendants("Table") On DirectCast(coursecategory.Element("GRP"), String) = DirectCast(category.Element("GRP"), String) _
        Where DirectCast(coursecategory.Element("RECTYPE"), String) = "C" _
        Select New Course()).ToList(Of Course)()
    

    主要区别在于最后一次选择。

        2
  •  0
  •   Richard Anthony Hein    15 年前

    由于演员阵容的原因,你得到了不同的结果。

    courseCategory.element(“rectype”).value返回的结果与
    (string)coursecategory.element(“rectype”)==“c”(显然,ctype(coursecategory.element(“rectype”),string)=“c”)。

    如果其中一个元素缺少子rectype节点,则如果正确地转换它(在vb.net中进行了转换),则不会有任何结果。

    在c中,您没有将(字符串)courseCategory.element(“rectype”)==“c”强制转换为右侧。如果使用ToString()或.Value,则会得到正确的结果。

    使用courseCategory.element(“rectype”).value代替,并完全避免强制转换。

    您可以通过将选择更改为返回来测试这一点:

    select new { 
    Wrong = (string)coursecategory.Element("RECTYPE"),  // No exception ... incorrect result!
    //Maybe = coursecategory.Element("RECTYPE").ToString() //throws NullReferenceException
    //Right = (string)coursecategory.Element("RECTYPE").Value // throws NullReferenceException because of a missing element.
    CategoryCode = category.Element("GRP").Value,      
    Code = course.Attribute("code").Value
    

    (});