简体   繁体   English

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

[英]Linq to Object gives “Specified argument was out of the range of valid values.”

I have some data that is retrieved as JSON from an API call. 我有一些从API调用中检索为JSON的数据。 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. 我试图选择第一个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;

This works...sometimes. 这有时...有效。 This runs inside a loop. 这在循环内运行。 The first iteration works, but subsequent iterations return 'false' even when they should return true. 第一次迭代有效,但是随后的迭代即使应返回true也返回“ false”。

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

Other attempts give Sequence contains no elements . 其他尝试给出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. 您可以先尝试检查d.ColData是否具有有效的项目集,然后再尝试执行“包含”测试之前检索第一个项目。

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. 我还要检查字符串(iedColData [0] .Value)的值是否不为null,否则在执行“包含”测试时会得到NullReferenceException。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:指定的参数超出有效值范围。 在RowDataBound Gridview中 - Error: Specified argument was out of the range of valid values. in RowDataBound Gridview 异常:指定的参数超出有效值的范围。 (参数“索引”) - Exception: Specified argument was out of the range of valid values. (Parameter 'index') 指定的参数超出了有效值的范围。 参数名称:值 - Specified argument was out of the range of valid values. Parameter name: value 指定的参数超出有效值范围。 参数名称:大小 - Specified argument was out of the range of valid values. Parameter name: size 得到“指定的参数超出有效值范围。 参数名称:” - Getting “Specified argument was out of the range of valid values. Parameter name:” 指定的参数超出有效值范围。 参数名称:site - Specified argument was out of the range of valid values. Parameter name: site System.ArgumentOutOfRangeException: &#39;指定的参数超出了有效值的范围。 参数名称:asp.net中的index&#39; - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net 指定的参数超出了有效值的范围。参数名称:site - 由于创建者更新而导致的错误 - Specified argument was out of the range of valid values. Parameter name: site - Error Due To Creators Update 指定的参数超出有效值范围。 参数名称:大小和串口通讯 - Specified argument was out of the range of valid values. Parameter name: size & Serial Port Communication FastMember:指定的参数超出有效值范围。 参数名称:名称 - FastMember: Specified argument was out of the range of valid values. Parameter name: name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM