Math.Max 处理三个可为 null 的值
Math.Max handle three nullable values
下面是示例xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<Match>
<IDCIBILDOBMatch>100</IDCIBILDOBMatch>
</Match>
<VerificationScore>
<IDDOBScore>180</IDDOBScore>
<IDAltDOBScore>60</IDAltDOBScore>
</VerificationScore>
我想取最大值,非空值,这样如果所有值都为空,则结果为空。
var dobMatchInformation = "";
var idALTDOBSCORE = "";
var idDOBScore = "";
XDocument document = XDocument.Parse(InputRequest);
bool Match_CIBILDOBMatch = document.Descendants("Match").Elements("IDCIBILDOBMatch").Any();
if (Match_CIBILDOBMatch == true)
{
dobMatchInformation = document.Descendants("Match").Elements("IDCIBILDOBMatch").FirstOrDefault().Value;
}
var dobscoreInformation = document.Descendants("VerificationScore");
bool Match_AltDOBScore = document.Descendants("VerificationScore").Elements("IDAltDOBScore").Any();
if (Match_AltDOBScore == true)
{
idALTDOBSCORE = dobscoreInformation.Select(x => x.Element("IDAltDOBScore").Value).Max();
}
bool Match_DOBScor = document.Descendants("VerificationScore").Elements("IDDOBScore").Any();
if (Match_DOBScor == true)
{
idDOBScore = dobscoreInformation.Select(x => x.Element("IDDOBScore").Value).Max();
}
var MAXDOBSCORE = Math.Max(Convert.ToInt32(dobMatchInformation), Math.Max(Convert.ToInt32(idALTDOBSCORE), Convert.ToInt32(idDOBScore)));
Math.Max 计算要处理空值。如果我当前的代码空值出现意味着得到错误
var MAXDOBSCORE = Math.Max(Convert.ToInt32(dobMatchInformation), Math.Max(Convert.ToInt32(idALTDOBSCORE), Convert.ToInt32(idDOBScore)));
首先,如果你有一个值,你只需要尝试解析为 int
,然后你可以将这些值保存在 List<int>
中,然后执行 Max
或任何你想做的事情想要如果它是空的。 XElement
也有一个显式转换为 int
,因此您不必获取 Value
然后转换它。
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;
下面是示例xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<Match>
<IDCIBILDOBMatch>100</IDCIBILDOBMatch>
</Match>
<VerificationScore>
<IDDOBScore>180</IDDOBScore>
<IDAltDOBScore>60</IDAltDOBScore>
</VerificationScore>
我想取最大值,非空值,这样如果所有值都为空,则结果为空。
var dobMatchInformation = "";
var idALTDOBSCORE = "";
var idDOBScore = "";
XDocument document = XDocument.Parse(InputRequest);
bool Match_CIBILDOBMatch = document.Descendants("Match").Elements("IDCIBILDOBMatch").Any();
if (Match_CIBILDOBMatch == true)
{
dobMatchInformation = document.Descendants("Match").Elements("IDCIBILDOBMatch").FirstOrDefault().Value;
}
var dobscoreInformation = document.Descendants("VerificationScore");
bool Match_AltDOBScore = document.Descendants("VerificationScore").Elements("IDAltDOBScore").Any();
if (Match_AltDOBScore == true)
{
idALTDOBSCORE = dobscoreInformation.Select(x => x.Element("IDAltDOBScore").Value).Max();
}
bool Match_DOBScor = document.Descendants("VerificationScore").Elements("IDDOBScore").Any();
if (Match_DOBScor == true)
{
idDOBScore = dobscoreInformation.Select(x => x.Element("IDDOBScore").Value).Max();
}
var MAXDOBSCORE = Math.Max(Convert.ToInt32(dobMatchInformation), Math.Max(Convert.ToInt32(idALTDOBSCORE), Convert.ToInt32(idDOBScore)));
Math.Max 计算要处理空值。如果我当前的代码空值出现意味着得到错误
var MAXDOBSCORE = Math.Max(Convert.ToInt32(dobMatchInformation), Math.Max(Convert.ToInt32(idALTDOBSCORE), Convert.ToInt32(idDOBScore)));
首先,如果你有一个值,你只需要尝试解析为 int
,然后你可以将这些值保存在 List<int>
中,然后执行 Max
或任何你想做的事情想要如果它是空的。 XElement
也有一个显式转换为 int
,因此您不必获取 Value
然后转换它。
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;