我有一组datacheck
,如下所示,
var lstDataCheck = new List<DataCheck>
{
new DataCheck{Name = "N1", CheckName = "Match1", Severity = "Low", Reason = "sample reason 1"},
new DataCheck{Name = "N2", CheckName = "Match1", Severity = "Low", Reason = "sample reason 2"},
new DataCheck{Name = "N3", CheckName = "Match2", Severity = "High", Reason = "sample reason 3"},
new DataCheck{Name = "N4", CheckName = "Match2", Severity = "High", Reason = "sample reason 4"},
new DataCheck{Name = "N5", CheckName = "Match3", Severity = "Medium", Reason = "sample reason 5"},
new DataCheck{Name = "N6", CheckName = "Match3", Severity = "Medium", Reason = "sample reason 6"}
};
现在有一组data
,我需要在name
和checkname
上与lstdatacheck
进行比较,还需要一些计数。
var lstData = new List<Data>
{
new Data{Name = "N1", CheckName = "Match1"},
new Data{Name = "N2", CheckName = "Match1"},
new Data{Name = "N3", CheckName = "Match2"},
new Data{Name = "N4", CheckName = "Match2"},
new Data{Name = "N5", CheckName = "Match3"},
};
这里是我为预期的FinalResult
所做的,
var lstSeverityData = new List<SeverityData>();
foreach (var data in lstData)
{
foreach (var chkData in lstDataCheck.Where(x => x.Name == data.Name))
{
if (chkData.CheckName == "Match1")
{
lstSeverityData.Add(new SeverityData { Severity = chkData.Severity, Reason = chkData.Reason });
}
if (chkData.CheckName == "Match2")
{
lstSeverityData.Add(new SeverityData { Severity = chkData.Severity, Reason = chkData.Reason });
}
}
}
var match1Count = lstData.Count(x => x.CheckName == "Match1");
var match2Count = lstData.Count(x => x.CheckName == "Match2");
var match3Count = lstData.Count(x => x.CheckName == "Match3");
var total = match1Count + match2Count + match3Count;
var final = new FinalResult
{
Count = total,
SeverityDatas = lstSeverityData,
MatchCount = new List<int>() { match1Count, match2Count, match3Count }
};
上面的代码根据我的要求给出了我期望的结果,但是我在这里看到了lstdata
上的多个枚举来获得我期望的结果。
问题,我如何最小化枚举或更好的方式编写以上代码?
下面是使用的不同类,
public class FinalResult
{
public int Count { get; set; }
public List<SeverityData> SeverityDatas { get; set; }
public List<int> MatchCount { get; set; }
}
public class SeverityData
{
public string Severity { get; set; }
public string Reason { get; set; }
}
public class Data
{
public string Name { get; set; }
public string CheckName { get; set; }
}
public class DataCheck
{
public string Name { get; set; }
public string CheckName { get; set; }
public string Severity { get; set; }
public string Reason { get; set; }
}
我确实同意Leszek Mazur的观点,它有点不清楚你在这里试图实现什么。但这是我第一次尝试。
using System.Collections.Generic;
using System.Linq;
namespace FasterCounter
{
class Program
{
static void Main(string[] args)
{
var lstDataCheck = new List<DataCheck>
{
new DataCheck{Name = "N1", CheckName = "Match1", Severity = "Low", Reason = "sample reason 1"},
new DataCheck{Name = "N2", CheckName = "Match1", Severity = "Low", Reason = "sample reason 2"},
new DataCheck{Name = "N3", CheckName = "Match2", Severity = "High", Reason = "sample reason 3"},
new DataCheck{Name = "N4", CheckName = "Match2", Severity = "High", Reason = "sample reason 4"},
new DataCheck{Name = "N5", CheckName = "Match3", Severity = "Medium", Reason = "sample reason 5"},
new DataCheck{Name = "N6", CheckName = "Match3", Severity = "Medium", Reason = "sample reason 6"}
};
var lstData = new List<Data>
{
new Data{Name = "N1", CheckName = "Match1"},
new Data{Name = "N2", CheckName = "Match1"},
new Data{Name = "N3", CheckName = "Match2"},
new Data{Name = "N4", CheckName = "Match2"},
new Data{Name = "N5", CheckName = "Match3"},
};
var finalResult = new FinalResult
{
MatchCount = new List<int>() { 0, 0, 0 },
SeverityDatas = new List<SeverityData>()
};
foreach (var data in lstData)
{
foreach (var chkData in lstDataCheck.Where(x => x.Name == data.Name))
{
switch (chkData.CheckName)
{
case "Match1":
finalResult.SeverityDatas.Add(new SeverityData { Severity = chkData.Severity, Reason = chkData.Reason });
finalResult.MatchCount[0]++;
break;
case "Match2":
finalResult.SeverityDatas.Add(new SeverityData { Severity = chkData.Severity, Reason = chkData.Reason });
finalResult.MatchCount[1]++;
break;
case "Match3":
finalResult.SeverityDatas.Add(new SeverityData { Severity = chkData.Severity, Reason = chkData.Reason });
finalResult.MatchCount[2]++;
break;
}
}
}
}
public class FinalResult
{
public int Count => MatchCount.Sum();
public List<SeverityData> SeverityDatas { get; set; }
public List<int> MatchCount { get; set; }
}
public class SeverityData
{
public string Severity { get; set; }
public string Reason { get; set; }
}
public class Data
{
public string Name { get; set; }
public string CheckName { get; set; }
}
public class DataCheck
{
public string Name { get; set; }
public string CheckName { get; set; }
public string Severity { get; set; }
public string Reason { get; set; }
}
}
}