2012-01-26 169 views
0
public class SomePropertyClass{ 
    public string VarA{get;set;} 
    public string VarB{get;set;} 
} 

SomePropertyClass v1 = new SomePropertyClass(){VarA = "item 1"}; 
SomePropertyClass v2 = new SomePropertyClass(){VarB = "item 2"}; 

是否有可能創建第三變量,將有:C#合併具有LINQ 2物體相同的2個對象

V3:翻= 「項目1」,VarB = 「項目2」

我的意思是,我想合併對象與linq到對象。

編輯
現在我需要從相同的類型。但將來按屬性名稱合併會很好。

我有一個帳戶模型,有很多用戶在步驟1中輸入的屬性。
我想將這個半滿模型與步驟2半滿模型合併。

編輯2

//step 1 
     GlobalOnBoardingDataModel step1= (GlobalOnBoardingDataModel)HttpContext.Current.Session[SessionVariableNameStepOne]; 
//step 2   
     GlobalOnBoardingDataModel step2 = (GlobalOnBoardingDataModel)HttpContext.Current.Session[SessionVariableNameStepTwo]; 



    class GlobalOnBoardingDataModel { 
     public string Email;//step 1 
     public string Name;//step 1 
     public string Phone;//step2 
     public string Address;//step2 
     } 
    } 

感謝

+3

'變種V3 =新SomePropertyClass {翻= v1.VarA,VarB = v2.VarB};'?你爲什麼要使用LINQ? –

+0

幫助我們更多地瞭解您的使用案例。你是否總是隻有兩個對象,一個是VarA集合,一個是VarB集合? – sblom

+0

@sblom,看我的編輯..謝謝 – SexyMF

回答

2

這裏是實現這個使用反射的方式:

public class SomePropertyClass 
{ 
    public string VarA { get; set; } 
    public string VarB { get; set; } 
} 

static class Program 
{ 
    static void Main(string[] args) 
    { 
     SomePropertyClass v1 = new SomePropertyClass() { VarA = "item 1" }; 
     SomePropertyClass v2 = new SomePropertyClass() { VarB = "item 2" }; 

     var yo = v1.Combine(v2); 
    } 

    static public IEnumerable<object> Combine<T, U>(this T one, U two) 
    { 
     var properties1 = one.GetType().GetProperties().Where(p => p.CanRead && p.GetValue(one, null) != null).Select(p => p.GetValue(one, null)); 
     var properties2 = two.GetType().GetProperties().Where(p => p.CanRead && p.GetValue(two, null) != null).Select(p => p.GetValue(two, null)); 

     return new List<object>(properties1.Concat(properties2)); 
    } 
} 
+0

這樣的庫,但是auto,不像建議的那樣。 – SexyMF

+0

更改了使用反射的答案以便提供一個自動機制 –

+0

謝謝,非常好 – SexyMF

3

你的意思是這樣......一個合併方法,該方法取值不爲null從匹配的屬性?

public class SomePropertyClass{ 
    public string VarA{get;set;} 
    public string VarB{get;set;} 

    public SomePropertyClass Merge (SomePropertyClass other) 
    { 
     return new SomePropertyClass 
        { VarA = this.VarA ?? other.VarA, 
         VarB = this.VarB ?? other.VarB 
        }; 
    } 

如果你想一個解決方案,將爲任何類,你就需要使用反射來發現所有的屬性,然後複製缺少的工作。 }

+0

所以沒有其他切肉刀自動合併它的東西? ( - :?? – SexyMF

+0

不是內置的,你需要使用反射或像http://valueinjecter.codeplex.com/ –

0

這裏的非LINQ的版本,但它會合並對象完美。試試凱爾芬利的Merge Two Objects into an Anonymous Type,它工作的很完美。

隨着TypeMerger合併很簡單,只要

var obj1 = new {foo = "foo"};

var obj2 = new {bar = "bar"};

var mergedObject = TypeMerger.MergeTypes(obj1 , obj2);

就是這樣,你得到的合併對象,除此之外,還有一項條款,忽視特定的屬性。

2

這裏的答案OP的問題是:

  • 一個確切的答案
  • 同一類型的兩個對象
    • (針對不同類型的兩個對象的代碼會幾乎相同)
  • 解決方案沒有硬編碼
  • 使用LINQ
  • 尊重源的非無效值
  • 不需要外部庫
  • 在一個代碼行。
public static T Merge<T>(T target, T source) 
{ 
    typeof(T) 
    .GetProperties() 
    .Select((PropertyInfo x) => new KeyValuePair<PropertyInfo, object>(x, x.GetValue(source, null))) 
    .Where((KeyValuePair<PropertyInfo, object> x) => x.Value != null).ToList() 
    .ForEach((KeyValuePair<PropertyInfo, object> x) => x.Key.SetValue(target, x.Value, null)); 

    //return the modified copy of Target 
    return target; 
} 
+1

「一行代碼」有點奢侈:) – A1rPun

+0

@ A1rPun,我猜'返回目標;'是一行代碼。所以,一行代碼ಠ‿ಠ – toddmo