2012-07-10 46 views
0

我有一个方法,我目前正在构建比较两个对象列表。该清单自身包含对象如何将一组对象和结果从一个方法返回

List<TradeFile> list1 
List<TradeFile> list2 

当我完成从列表1和TradeFile对象从列表2比较TradeFile对象,我想返回一个包含所有比较TradeFiles和匹配状态的集合。因此,这将是这样的:

TradeFile1,TradeFile2,真 TradeFile1,TradeFile2,假

我会用这个集合,然后供以后在我的过程报告。我应该看看使用包含交易文件对象集合的字典吗?

这可以工作,但真的感觉凌乱:

Dictonary<Dictonary<TradeFile,TradeFile>,bool> 

编辑: 这是它结束了基于一些下面的答案看起来像。

private List CompareTradeFileObject(List list1,List list2) { List results = 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; } 
} 

回答

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 
3

最简洁的方法是为返回类型创建一个类并返回该类的列表。

或者您可以使用元组类这样的:

List<Tuple<TradeFile, TradeFile, bool>> 
0

如果贸易文件有一个ID,你可以只返回

List<int, bool> 

INT是唯一的ID,布尔是如果它等于

相关问题