【问题标题】:C# Merge 2 objects of the same with linq 2 objectC# 将 2 个与 linq 2 对象相同的对象合并
【发布时间】:2012-01-26 08:15:18
【问题描述】:
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"};

是否可以创建第三个变量:

v3: VarA = "项目 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
        }
    }

谢谢

【问题讨论】:

  • var v3 = new SomePropertyClass { VarA = v1.VarA, VarB = v2.VarB }; ?为什么要使用 LINQ?
  • 帮助我们更多地了解您的用例。您是否总是有两个对象,一个带有 VarA 集,一个带有 VarB 集?
  • 那请给我们一个完整的例子!
  • 您确定这是个好主意吗?看起来你会更好,只需在会话中存储 1 个对象,并在“step1”期间写入属性的一个子集,在“step2”期间写入另一个子集。
  • @JonasH,MVC 正在反序列化它,这意味着我需要加入它们,我无法告诉 mvc 对现有对象进行反序列化。

标签: c# linq merge


【解决方案1】:

以下是 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;
}

【讨论】:

  • @A1rPun,我猜return target; 是一行代码。所以,一行漂亮的代码ಠ‿ಠ
【解决方案2】:

你的意思是这样的...... Merge 方法从匹配的属性中获取任何不为空的值?

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 
                    };
    }

如果您想要一个适用于 任何 类的解决方案,您需要使用反射来查找所有属性,然后复制缺失的属性。 }

【讨论】:

  • 那么没有其他的切割器自动合并它吗? (-: ?
  • 不是内置的,你需要使用反射或者像valueinjecter.codeplex.com这样的库
【解决方案3】:

这是一种使用反射来完成此操作的方法:

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));
    }
}

【讨论】:

  • 将答案更改为使用反射以提供自动机制。
猜你喜欢
  • 2021-01-11
  • 2020-11-28
  • 2021-10-29
  • 1970-01-01
  • 2015-08-23
  • 2018-10-07
  • 2018-03-04
  • 1970-01-01
  • 2011-11-01
相关资源
最近更新 更多