Linq to Object给出"指定的参数超出了有效值的范围。"

时间:2018-02-15 18:59:41

标签: c# json linq

我有一些数据是从API调用中检索为JSON的。检索数据并将其转换为对象,但我无法可靠地访问我需要的数据。我需要查询对象以确定是否存在值,但是当该值不存在时,我会收到各种错误消息。我试图选择第一个ColData等于给定字符串的行。

// When the string does not exist I get 
// Specified argument was out of the range of valid values.
var t1 = PriorMonthTB.Rows.Row.Where( d => d.ColData[ 0 ].Value.Contains( "sdfasfd" ) ).Any(); 
var t2 = PriorMonthTB.Rows.Row.FirstOrDefault( d => d.ColData[ 0 ].Value.Contains( "sdfasfd" ) );
var t3 = PriorMonthTB.Rows.Row.Any( d => d.ColData[ 0 ].Value.Contains( "sdfasfd" ) );
var t5 = PriorMonthTB.Rows.Row.Where( r => r.ColData[ 0 ].Value == "sdfasfd" ).FirstOrDefault().ColData.FirstOrDefault().Value;

这有效......有时候。这在一个循环中运行。第一次迭代起作用,但随后的迭代返回“假”'即使他们应该回归真实。

var t4 = PriorMonthTB.Rows.Row.Select( d => d.ColData[ 0 ].Value.Contains( "sdfasfd" ) ).FirstOrDefault();

其他尝试提供Sequence contains no elements

我尝试查询的对象看起来像这样

public partial class TrialBalance : RealmObject
{        
    [JsonProperty( "Header" )]
    public Header Header { get; set; }

    [JsonProperty( "Columns" )]
    public Columns Columns { get; set; }

    [JsonProperty( "Rows" )]
    public Rows Rows { get; set; }
}

public partial class Columns : RealmObject
{
    [JsonProperty( "Column" )]
    public IList<Column> Column { get; }
}

public partial class Column : RealmObject
{
    [JsonProperty( "ColTitle" )]
    public string ColTitle { get; set; }

    [JsonProperty( "ColType" )]
    public string ColType { get; set; }
}

public partial class Header : RealmObject
{
    [JsonProperty( "Time" )]
    public DateTimeOffset Time { get; }

    [JsonProperty( "ReportName" )]
    public string ReportName { get; set; }

    [JsonProperty( "DateMacro" )]
    public string DateMacro { get; set; }

    [JsonProperty( "ReportBasis" )]
    public string ReportBasis { get; set; }

    [JsonProperty( "StartPeriod" )]
    public DateTimeOffset StartPeriod { get; set; }

    [JsonProperty( "EndPeriod" )]
    public DateTimeOffset EndPeriod { get; set; }

    [JsonProperty( "Currency" )]
    public string Currency { get; set; }

    [JsonProperty( "Option" )]
    public IList<Option> Option { get; }
}

public partial class Option : RealmObject
{
    [JsonProperty( "Name" )]
    public string Name { get; set; }

    [JsonProperty( "Value" )]
    public string Value { get; set; }
}

public partial class Rows : RealmObject
{
    [JsonProperty( "Row" )]
    public IList<Row> Row { get; }
}

public partial class Row : RealmObject
{
    [JsonProperty( "ColData" )]
    public IList<RowColDatum> ColData { get; }

    [JsonProperty( "Summary" )]
    public Summary Summary { get; set; }

    [JsonProperty( "type" )]
    public string Type { get; set; }

    [JsonProperty( "group" )]
    public string Group { get; set; }
}

public partial class RowColDatum : RealmObject
{
    [JsonProperty( "value" )]
    public string Value { get; set; }

    [JsonProperty( "id" )]
    public string Id { get; set; }
}

public partial class Summary : RealmObject
{
    [JsonProperty( "ColData" )]
    public IList<SummaryColDatum> ColData { get; }
}

public partial class SummaryColDatum : RealmObject
{
    [JsonProperty( "value" )]
    public string Value { get; set; }
}

我并不认为这会让人头痛,但我无法让它发挥作用。有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:2)

在尝试在执行“包含”测试之前检索第一个项目之前,您可以尝试检查d.ColData是否有一组有效的项目。

我还会检查字符串的值(即d.ColData [0] .Value)是否为null,否则当您执行'Contains'测试时,您将获得NullReferenceException。

var t1 = PriorMonthTB.Rows.Row.Any(d => d.ColData.Any() &&
                                        d.ColData[0].Value != null &&
                                        d.ColData[0].Value.Contains("sdfasfd"));