我有一些数据是以JSON的形式从API调用中检索的。数据被检索并转换为对象,但我很难可靠地访问所需的数据。我需要查询对象以确定值是否存在,但是当值不存在时,我会收到各种错误消息。我试图选择第一个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;
这个works...sometimes。这在一个循环中运行。第一次迭代工作,但后续迭代返回“false”,即使它们应该返回true。
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; }
}
我不认为这会如此头痛,但我无法让它开始工作。有人能告诉我我做错了什么吗?
发布于 2018-02-15 11:26:08
在执行“Contains”测试之前,您可以尝试在尝试检索第一个项之前检查d.ColData是否有一组有效的项。
我还会检查字符串(即d.ColData.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"));
https://stackoverflow.com/questions/48814455
复制相似问题