我正在构建一个方法来比较两个对象列表。列表本身包含对象
List<TradeFile> list1
List<TradeFile> list2
当我完成list1中的TradeFile对象和list2中的TradeFile对象的比较时,我想返回一个包含所有比较的TradeFiles和匹配状态的集合。所以它会是这样的:
TradeFile1,TradeFile2,True TradeFile1,TradeFile2,False
我将使用此集合然后在我的过程中报告。我应该看一下使用包含tradefile对象集合的字典吗?
这可能有效,但确实感觉很乱:
Dictonary<Dictonary<TradeFile,TradeFile>,bool>
编辑: 基于下面的答案,这就是最终的结果。
私人列表CompareTradeFileObject(列表list1,列表list2) { 列出结果= new List(); bool matches = false;
if (list1.Count == list2.Count)
{
list1.Sort((x, y) => x.FormatName.CompareTo(y.FormatName));
list2.Sort((x, y) => x.FormatName.CompareTo(y.FormatName));
for (int i = 0; i < list1.Count; i++)
{
TradeFileCompare tf = new TradeFileCompare();
tf.TradeFile1 = list1[i];
tf.TradeFile2 = list2[i];
if (list1[i].FileExtension == list2[i].FileExtension && list1[i].FormatName == list2[i].FormatName &&
list1[i].GroupName == list2[i].GroupName && list1[i].MasterAccountId == list2[i].MasterAccountId)
{
matches = CompareTradeFileContents(list1[i].FileContents, list2[i].FileContents);
tf.IsMatch = matches;
}
else
{
tf.IsMatch = matches;
}
results.Add(tf);
}
}
else
{
matches = false;
}
return results;
}
class TradeFileCompare
{
public TradeFile TradeFile1 { get; set; }
public TradeFile TradeFile2 { get; set; }
public bool IsMatch { get; set; }
}
答案 0 :(得分:3)
最干净的方法是为返回类型创建一个类并返回此类的列表。
或者您可以使用Tuple类:
List<Tuple<TradeFile, TradeFile, bool>>
答案 1 :(得分:2)
void Main()
{
var list1 = new List<TradeFile>(new [] {
new TradeFile { Name = "TradeFile1", Data = "a" },
new TradeFile { Name = "TradeFile2", Data = "b" },
});
var list2 = new List<TradeFile>(new [] {
new TradeFile { Name = "TradeFile4", Data = "a" },
new TradeFile { Name = "TradeFile5", Data = "c" },
});
var query = from tradeFile1 in list1
from tradeFile2 in list2
select new TradeFileComparison(tradeFile1, tradeFile2);
foreach (var item in query)
{
Console.WriteLine(item.ToString());
}
}
class TradeFile
{
public string Name { get; set; }
public string Data { get; set; }
public bool Matches(TradeFile otherTradeFile)
{
return (this.Data == otherTradeFile.Data);
}
}
class TradeFileComparison
{
public TradeFileComparison(TradeFile tradeFile1, TradeFile tradeFile2)
{
this.TradeFile1 = tradeFile1;
this.TradeFile2 = tradeFile2;
}
public TradeFile TradeFile1 { get; set; }
public TradeFile TradeFile2 { get; set; }
bool IsMatch { get { return this.TradeFile1.Matches(TradeFile2); } }
public override string ToString()
{
return string.Format("{0}, {1}, {2}",
this.TradeFile1.Name, this.TradeFile2.Name,
this.IsMatch.ToString());
}
}
<强>输出:强>
TradeFile1, TradeFile4, True
TradeFile1, TradeFile5, False
TradeFile2, TradeFile4, False
TradeFile2, TradeFile5, False
答案 2 :(得分:0)
如果交易文件有id,你可以返回
List<int, bool>
int是唯一的id,bool是否相等