代码之家  ›  专栏  ›  技术社区  ›  Keith Adler

不能在LINQ查询中使用三元运算符

  •  3
  • Keith Adler  · 技术社区  · 16 年前

    Object reference not set to an instance of an object.

    var courses = from d in somesource
                              orderby d.SourceName, d.SourceType
                              select new
                              {
                                  ID = d.InternalCode,
                                  Name = string.Format("{0} - {1}{2}", d.InternalCode, d.SourceName, (d.SourceType.Length > 0 ? ", " + d.SourceType : string.Empty))
                              };
    

    有什么想法吗?

    3 回复  |  直到 16 年前
        1
  •  8
  •   SLaks    16 年前

    d.SourceType null .

    你应该打电话

    (String.IsNullOrEmpty(d.SourceType) ? ", " + d.SourceType : string.Empty)
    
        2
  •  1
  •   Adam Robinson    16 年前

    你在检查 Length 财产 SourceType

        3
  •  1
  •   Marko    16 年前

    可能SourceType为null,所以在d.SourceType.Length上会出现异常。