2014-07-09 124 views
4

我想将具有不同属性的两个列表合并到一个列表中,但在合并它时,我想检查在这个特定示例中是否存在两个列表中的确切日期,以及if还有就是,我想利用这两个属性从元素,并将它们合并到一个元件中的另一个列表合并C#中的两个列表

表1:

List<object> r1 = (from x in sp1 select new 
        { 
        x.Imported, 
        x.Period 
        }).ToList<object>(); 

L1结果:

enter image description here

列表2:

List<object> r2 = (from x in sp2 select new 
        { 
        x.Dissolution, 
        x.Period 
        }).ToList<object>(); 

L2结果:

enter image description here


求购结果:

enter image description here

现在,我这是怎么合并R1和R2:

List<object> r3 = new List<object>(r1.Concat(r2)); 
+2

请使用强类而不是匿名类。 –

+1

这与问题无关;) – ToTa

+1

我认为这里的堆栈的目标之一是挑战:) –

回答

0

创建字典

Dictionary MyDict<String, List<Object>>; 

MyDict[object.Perdiod].Add(object); 

对于每一天都会有在dictionnary的条目,并它将在这个“日期索引”中保存在那个时期发生的所有对象的列表。

最简单的方式国际海事组织和它不需要作O(n)的检查每个条目添加

只要确保当您添加数据,这不是空IE

MyDict[Object.Period] != null 

另外, Nikhil Agrawal说我不会使用Object来保存一个事物列表......它感觉不对,并且容易出错。你可能想要声明一个抽象类,它将像界面一样使用,或者只是这些项目(对象)的接口。

2

你可以将其转化为相同类型和使用这样的东西

r1 
.Select(x => new { Imported = x.Imported, Dissolution = null, Period = x.Period) 
.Concat(
    r2.Select(x => new { Imported = null, Dissolution = x.Dissolution, Period = x.Period)) 
.GroupBy(x => x.Period) 
.Select(x => new { Imported = x.Max(e => e.Imported), 
        Dissolution = x.Max(e => e.Dissolution), 
        Period = x.Key); 
0

阿法克需要反射来实现这一目标的名称为匿名类型在编译的时候在这里你如何能做到的例子被分配什么你想

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using System.Text; 
using System.Xml.Linq; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<ImportedType> sp1 = new List<ImportedType>(); 
      List<DissolutionType> sp2 = new List<DissolutionType>(); 
      sp1.AddRange(new ImportedType[]{new ImportedType() { Imported = 2, Period = "2024-02" }, new ImportedType() { Imported = 2, Period = "2014-11" }, new ImportedType() { Imported = 2, Period = "2024-12" }}); 
      sp2.AddRange(new DissolutionType[] { new DissolutionType() { Dissolution = 2, Period = "2024-02" }, new DissolutionType() { Dissolution = 2, Period = "2034-02" }, new DissolutionType() { Dissolution = 2, Period = "2024-12" } });  
      var r1 = (from x in sp1 
           select new 
           { 
            x.Imported, 
            x.Period 
           }).ToList<object>(); 
      var r2 = (from x in sp2 
           select new 
           { 
            x.Dissolution, 
            x.Period 
           }).ToList<object>();     
      var r3 = r1.Concat(r2).Except(r1.Where(res => 
       {  
        object vp2 = r2.SingleOrDefault(res2 => GetValue(res2) == GetValue(res)); 
        if (vp2!=null) 
        { 
         return true; 
        }  
        return false; 
       }));   
     } 

     private static object GetValue(object res) 
     { 
      Type t = res.GetType(); 
      PropertyInfo p = t.GetProperty("Period"); 
      object v = p.GetValue(res, null); 
      return v; 
     } 
    } 
} 

//这里我想你实现2类这样

public class ImportedType 
    { 
     public int Imported { get; set; } 
     public string Period { get; set; } 

    } 
    public class DissolutionType 
    { 
     public int Dissolution { get; set; } 
     public string Period { get; set; } 

    } 

结果 enter image description here

+0

我看到了这个,但是我需要这个结果http://i.imgur.com/SqTIRd2.png – ToTa

0

我尼基尔阿格拉瓦尔在同意现在的代码确实需要一些固定起来,因为它是真的很难用匿名类型的工作,特别是因为他们已被转换为对象。

忽略这一点,接受它作为一种挑战(使用匿名类型强制转换为对象),这是我想出了:该合并不完全外

代码加入:

 Func<object, object> getPeriodKey = first => 
     { 
      var periodProperty = first.GetType().GetProperty("Period"); 
      return periodProperty.GetValue(first); 
     }; 

     var temp = r1.GroupJoin(r2, getPeriodKey, getPeriodKey, (obj, tInner) => 
     { 
      dynamic right = tInner.FirstOrDefault(); 

      if (right == null) 
       return (object)(new 
       { 
        Period = ((dynamic)obj).Period, 
        Imported = ((dynamic)obj).Imported, 
       }); 
      else 
       return (object)(new 
       { 
        Period = ((dynamic)obj).Period, 
        Imported = ((dynamic)obj).Imported, 
        Dissolution = (int?)right.Dissolution, 
       }); 

     }); 

     var merged = temp.Union(r2, new RComparer()); 

以及所需的比较器如下:

class RComparer : IEqualityComparer<object> 
    { 
     public bool Equals(object x, object y) 
     { 
      var xPeriodProperty = x.GetType().GetProperty("Period"); 
      var yPeriodProperty = y.GetType().GetProperty("Period"); 

      if (xPeriodProperty != null && yPeriodProperty != null) 
      { 
       var xPeriod = (string)xPeriodProperty.GetValue(x); 
       var yPeriod = (string)yPeriodProperty.GetValue(y); 
       return xPeriod == yPeriod; 
      } 
      else 
       return base.Equals(y); 
     } 

     public int GetHashCode(object obj) 
     { 
      var periodProperty = obj.GetType().GetProperty("Period"); 

      if (periodProperty != null) 
       //This will essentially hash the string value of the Period 
       return periodProperty.GetValue(obj).GetHashCode(); 
      else 
       return obj.GetHashCode(); 
      ; 
     } 
    }