I have some data that is retrieved as JSON from an API call. The data is retrieved and converted to an object but I'm having trouble reliably accessing the data that I need. I need to query the object to determine if a value exists, but when the value does not exist I get various errors messages. I'm trying to select the Row where the first ColData equals a given string.
// 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;
This works...sometimes. This runs inside a loop. The first iteration works, but subsequent iterations return 'false' even when they should return true.
var t4 = PriorMonthTB.Rows.Row.Select( d => d.ColData[ 0 ].Value.Contains( "sdfasfd" ) ).FirstOrDefault();
Other attempts give Sequence contains no elements
.
The object I'm trying to query looks like this
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; }
}
I didn't think this would be such a headache but I can't get it to work. Can someone show me what I'm doing wrong?
You could try checking that d.ColData has a valid set of items before attempting to retrieve the first item prior to performing the 'Contains' test.
I'd also check that the value of the string (iedColData[0].Value) isn't null otherwise the you'll get a NullReferenceException when you perform the 'Contains' test.
var t1 = PriorMonthTB.Rows.Row.Any(d => d.ColData.Any() &&
d.ColData[0].Value != null &&
d.ColData[0].Value.Contains("sdfasfd"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.