Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Max处理三个可为空的值_C#_Xml_Linq To Xml - Fatal编程技术网

C# Max处理三个可为空的值

C# Max处理三个可为空的值,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,下面是示例xml文件代码 <?xml version="1.0" encoding="utf-8"?> <Match> <IDCIBILDOBMatch>100</IDCIBILDOBMatch> </Match> <VerificationScore> <IDDOBScore>180</IDDOBScore> <IDAltDOBSco

下面是示例xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<Match>
  <IDCIBILDOBMatch>100</IDCIBILDOBMatch>
</Match>
<VerificationScore>
    <IDDOBScore>180</IDDOBScore>
    <IDAltDOBScore>60</IDAltDOBScore>
</VerificationScore>
Max计算想要处理空值。如果我的当前代码为空值,则表示出现错误

var MAXDOBSCORE = Math.Max(Convert.ToInt32(dobMatchInformation), Math.Max(Convert.ToInt32(idALTDOBSCORE), Convert.ToInt32(idDOBScore)));

首先,如果有值,只需尝试解析为
int
,然后可以将值保存在
列表中,然后执行
Max
,如果值为空,则执行任何操作。而且
XElement
有一个显式转换为
int
,因此您不必获取
值,然后将其转换

XDocument document = XDocument.Parse(InputRequest);
var values = new List<int>();

bool Match_CIBILDOBMatch = document.Descendants("Match").Elements("IDCIBILDOBMatch").Any();
if (Match_CIBILDOBMatch == true)
{
    values.Add((int)dobMatchInformation = document.Descendants("Match").Elements("IDCIBILDOBMatch").FirstOrDefault());
}

var dobscoreInformation = document.Descendants("VerificationScore");
bool Match_AltDOBScore = document.Descendants("VerificationScore").Elements("IDAltDOBScore").Any();
if (Match_AltDOBScore == true)
{
    values.Add(dobscoreInformation.Select(x => (int)x.Element("IDAltDOBScore")).Max());
}

bool Match_DOBScor = document.Descendants("VerificationScore").Elements("IDDOBScore").Any();
if (Match_DOBScor == true)
{
    values.Add(idDOBScore = dobscoreInformation.Select(x => (int)x.Element("IDDOBScore")).Max());
}

var MAXDOBSCORE = values.Any() ? (int?)values.Max() : null;
XDocument document=XDocument.Parse(InputRequest);
var值=新列表();
bool Match_CIBILDOBMatch=document.substands(“Match”).Elements(“IDCIBILDOBMatch”).Any();
if(Match_CIBILDOBMatch==true)
{
values.Add((int)dobMatchInformation=document.substands(“Match”).Elements(“IDCIBILDOBMatch”).FirstOrDefault());
}
var dobscoreInformation=document.substands(“VerificationScore”);
bool Match_AltDOBScore=document.substands(“VerificationScore”).Elements(“IDAltDOBScore”).Any();
如果(匹配分数==真)
{
Add(dobscoreInformation.Select(x=>(int)x.Element(“IDAltDOBScore”)).Max();
}
bool Match_DOBScor=document.substands(“VerificationScore”).Elements(“IDDOBScore”).Any();
if(Match_DOBScor==true)
{
Add(idDOBScore=dobscoreInformation.Select(x=>(int)x.Element(“idDOBScore”)).Max();
}
var MAXDOBSCORE=values.Any()?(int?)值。Max():null;

如果这些值为
null
则问题在于将失败的
Convert.Int32
。添加
null
检查或切换到
int.TryParse
。当您成功解析一个值时,将其添加到
列表中,然后对该值调用
Max
,或者如果该值为空,则使用
null
int?
。我尝试了,但没有工作。你能修改我的代码@juharr吗
XDocument document = XDocument.Parse(InputRequest);
var values = new List<int>();

bool Match_CIBILDOBMatch = document.Descendants("Match").Elements("IDCIBILDOBMatch").Any();
if (Match_CIBILDOBMatch == true)
{
    values.Add((int)dobMatchInformation = document.Descendants("Match").Elements("IDCIBILDOBMatch").FirstOrDefault());
}

var dobscoreInformation = document.Descendants("VerificationScore");
bool Match_AltDOBScore = document.Descendants("VerificationScore").Elements("IDAltDOBScore").Any();
if (Match_AltDOBScore == true)
{
    values.Add(dobscoreInformation.Select(x => (int)x.Element("IDAltDOBScore")).Max());
}

bool Match_DOBScor = document.Descendants("VerificationScore").Elements("IDDOBScore").Any();
if (Match_DOBScor == true)
{
    values.Add(idDOBScore = dobscoreInformation.Select(x => (int)x.Element("IDDOBScore")).Max());
}

var MAXDOBSCORE = values.Any() ? (int?)values.Max() : null;