重複の可能性:
2 つのリストを比較して違いを確認する
これは私の機能です
public List<String[]> comparableListsAreTheSame(List<String> baseList, List<String> resultList, int type)
{
if (type == 1) { }
List<String> baseListCopy = baseList;
List<String> resultListCopy = resultList;
bool sameLength = (baseListCopy.Count == resultList.Count); // are 2 lists have the same length?
List<String> Base = baseListCopy.Except(resultListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values
List<String> Result = resultListCopy.Except(baseListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values
List<String[]> blocksComparisonSet1 = new List<String[]>(); //we add blocks based on list1; so we could output them to excel
List<String[]> blocksComparisonSet2 = new List<String[]>(); //we add blocks based on list2; so we could output them to excel
List<String[]> blocksComparisonFinal = new List<String[]>(); //we combine list1 and list
//-----------------------------------------------------------------
if (Result.Count > 0 || Base.Count > 0)
{
foreach (String resultLine in Result) //loop over all lines in list 1
{
bool found = false; //if element in base i
String[] resultLineArray = resultLine.Split('*'); //get array from the string
foreach (String baseLine in Base)
{
String[] baseLineArray = baseLine.Split('*');
if (resultLineArray[0].Equals(baseLineArray[0]) && resultLineArray[1].Equals(baseLineArray[1]) && resultLineArray[2].Equals(baseLineArray[2]) && resultLineArray[3].Equals(baseLineArray[3]))
{
String[] NA = new String[2]; //keep results
NA[0] = baseLine; //[0] for base
NA[1] = resultLine; //[1] for result
blocksComparisonSet1.Add(NA);
found = true;
}
}
if (!found)
{
String[] NA = new String[2]; //keep results
NA[0] = "N/A"; //[0] for base
NA[1] = resultLine; //[1] for result
blocksComparisonSet1.Add(NA);
}
}
//-----------------------------------------------------------------
foreach (String baseLine in Base) //loop over all lines in list 2
{
bool found = false; //if element in base i
String[] baseLineArray = baseLine.Split('*'); //get array from the string
foreach (String resultLine in Result)
{
String[] resultLineArray = resultLine.Split('*');
if (resultLineArray[0].Equals(baseLineArray[0]) && resultLineArray[1].Equals(baseLineArray[1]) && resultLineArray[2].Equals(baseLineArray[2]) && resultLineArray[3].Equals(baseLineArray[3]))
{
String[] NA = new String[2]; //keep results
NA[0] = baseLine; //[0] for base
NA[1] = resultLine; //[1] for result
blocksComparisonSet2.Add(NA);
found = true;
}
}
if (!found)
{
String[] NA = new String[2]; //keep results
NA[0] = baseLine; //[0] for base
NA[1] = "N/A"; //[1] for result
blocksComparisonSet2.Add(NA);
}
}
}
//-----------------------------------------------------------------
if (blocksComparisonSet1.Any() || blocksComparisonSet2.Any()) //check if we have any values in out differences lists. if we do, merge them
{
blocksComparisonFinal.AddRange(blocksComparisonSet1); //add records from one list to final list
blocksComparisonFinal.AddRange(blocksComparisonSet2); //add records from second list to final list
HashSet<String[]> s = new HashSet<String[]>(blocksComparisonFinal);
blocksComparisonFinal = s.ToList();
}
blocksComparisonFinal = blocksComparisonSet1.Union(blocksComparisonSet2, new ArrayEqualityComparer<string>()).ToList();
return blocksComparisonFinal;
}
私は一般的にC#とプログラミングが初めてで、複数のループを実行し、すべてをかなり野蛮な方法で一致させました。より専門的な方法でアプローチし、よりクリーンで適切に行うことはできますか?