7

我已经考虑过如何通过推出自己的解决方案来解决这个问题,但我想知道 .NET 是否已经具备我想要实现的功能 - 如果是这样,我宁愿使用内置的东西.

假设我有一个Widget对象的两个实例,我们称它们为PartAand PartB。每个人的信息都是从两个不同的 Web 服务中获取的,但两者都有匹配的 ID。

PartA
{
    ID: 19,
    name: "Percy",
    taste: "",
    colour: "Blue",
    shape: "",
    same_same: "but different"
}

PartB
{
    ID: 19,
    name: "",
    taste: "Sweet",
    colour: "",
    shape: "Hexagon",
    same_same: "but not the same"
}

我想合并这些以创建以下内容:

Result
{
    ID: 19,
    name: "Percy",
    taste: "Sweet",
    colour: "Blue",
    shape: "Hexagon",
    same_same: "but different"
}

注意same_same每个之间的值是如何不同的,但我们认为 PartA 为主,所以结果保留了 value but different

现在让事情复杂化:

假设我们有两个列表:

List<Widget> PartA = getPartA();
List<Widget> PartB = getPartB();

现在这里有一些伪代码描述我想要做什么:

List<Widget> Result = PartA.MergeWith(PartB).MergeObjectsOn(Widget.ID).toList();
4

3 回答 3

13

您可以编写自己的扩展方法,如下所示:

static class Extensions
{
    public static IEnumerable<T> MergeWith<T>(this IEnumerable<T> source, IEnumerable<T> other) where T : ICanMerge
    {
        var otherItems = other.ToDictionary(x => x.Key);
        foreach (var item in source)
        {
            yield return (T)item.MergeWith(otherItems[item.Key]);
        }
    }
    public static string AsNullIfEmpty(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return null;
        else
            return s;
    }
}

哪里ICanMerge像:

public interface ICanMerge
{
    object Key { get; }
    ICanMerge MergeWith(ICanMerge other);
}

实施例如:

public class Widget : ICanMerge
{
    object ICanMerge.Key { get { return this.ID; } }
    int ID {get;set;}
    string taste {get;set;}
    public ICanMerge MergeWith(ICanMerge other)
    {
        var merged = new Widget();
        var otherWidget = (Widget)other;
        merged.taste = this.taste.AsNullIfEmpty() ?? otherWidget.taste;
        //...
        return merged;
    }
}

然后就很简单了PartA.MergeWith(PartB).ToList()

于 2012-09-04T01:02:33.743 回答
2

如果您的列表是一对一的(即相同数量的项目,并且 PartA 列表中的每个项目在 PartB 列表中都有匹配项),那么我会考虑使用Zip扩展方法。请注意,Zip 实际上并不要求每个列表具有相同数量的项目。但是,如果您不能依赖“配对”具有匹配 ID 的项目,那么我的简单方法将行不通。

你可以这样做:

var alist = GetPartAWidgets().OrderBy(w => w.ID);
var blist = GetPartBWidgets().OrderBy(w => w.ID);
var merged = alist.Zip(blist, (a,b) => new Widget()
             {
               ID = a.ID,
               Name = string.IsNullOrEmpty(a.Name) ? b.Name : a.Name,
               //etc. 
             });

如果您希望您的 linq 看起来更干净,您可以将单个 Widget 合并逻辑封装在一个函数或扩展方法中,并使用它而不是内联委托。

于 2012-09-04T02:00:42.760 回答
0
   public interface IMerge<out T>
{
    IEnumerable<IMergeMatched<T>> Matched();

    IEnumerable<IMergeMatched<T>> Matched(Func<T, T, bool> predicate);

    IEnumerable<T> NotMatchedBySource();

    IEnumerable<T> NotMatchedBySource(Func<T, bool> predicate);

    IEnumerable<T> NotMatchedByTarget();

    IEnumerable<T> NotMatchedByTarget(Func<T, bool> predicate);
}

public interface IMergeMatched<out T>
{
    T Source { get; }

    T Target { get; }
}

public static class Enumerable
{
    public static IMerge<TSource> Merge<TSource>(this IEnumerable<TSource> source, IEnumerable<TSource> target,
                                             Func<TSource, TSource, bool> predicate)
    {
        return new Merge<TSource>(source, target, predicate);
    }
}

public class Merge<T> : IMerge<T>
{
    private readonly Func<T, T, bool> _predicate;
    private readonly IEnumerable<T> _source;
    private readonly IEnumerable<T> _target;
    private IEnumerable<IMergeMatched<T>> _matcheds;
    private IEnumerable<T> _notMatchedBySource;
    private IEnumerable<T> _notMatchedByTarget;

    public Merge(IEnumerable<T> source, IEnumerable<T> taget, Func<T, T, bool> predicate)
    {
        _source = source;
        _target = taget;
        _predicate = predicate;
    }

    public IEnumerable<IMergeMatched<T>> Matched()
    {
        if (_matcheds == null)
        {
            Analize();
        }
        return _matcheds;
    }

    public IEnumerable<IMergeMatched<T>> Matched(Func<T, T, bool> predicate)
    {
        return Matched()
            .Where(t => predicate.Invoke(t.Source, t.Target))
            .ToArray();
    }

    public IEnumerable<T> NotMatchedBySource()
    {
        if (_notMatchedBySource == null)
        {
            Analize();
        }
        return _notMatchedBySource;
    }

    public IEnumerable<T> NotMatchedBySource(Func<T, bool> predicate)
    {
        return NotMatchedBySource()
            .Where(predicate)
            .ToArray();
    }

    public IEnumerable<T> NotMatchedByTarget()
    {
        if (_notMatchedByTarget == null)
        {
            Analize();
        }
        return _notMatchedByTarget;
    }

    public IEnumerable<T> NotMatchedByTarget(Func<T, bool> predicate)
    {
        return NotMatchedByTarget()
            .Where(predicate)
            .ToArray();
    }

    private void Analize()
    {
        var macheds = new List<MergeMached<T>>();
        var notMachedBySource = new List<T>(_source);
        var notMachedByTarget = new List<T>(_target);

        foreach (var source in _source)
        {
            foreach (var target in _target)
            {
                var macth = _predicate.Invoke(source, target);
                if (!macth) continue;

                macheds.Add(new MergeMached<T>(source, target));
                notMachedBySource.Remove(source);
                notMachedByTarget.Remove(target);
            }
        }

        _matcheds = macheds.ToArray();
        _notMatchedBySource = notMachedBySource.ToArray();
        _notMatchedByTarget = notMachedByTarget.ToArray();
    }
}

public class MergeMached<T> : IMergeMatched<T>
{
    public MergeMached(T source, T target)
    {
        Source = source;
        Target = target;
    }

    public T Source { get; private set; }

    public T Target { get; private set; }
}

如何使用?

var source = new List<MediaFolder>
            {
                new MediaFolder
                    {
                        Id = "Id1",
                        Name = "Name1",
                        Path = "Path1"
                    },
                new MediaFolder
                    {
                        Id = "Id2",
                        Name = "Name2",
                        Path = "Path2"
                    },
                new MediaFolder
                    {
                        Id = "Id3",
                        Name = "Name3",
                        Path = "Path3"
                    },
                new MediaFolder
                    {
                        Id = "Id4",
                        Name = "Name4",
                        Path = "Path4"
                    },
                new MediaFolder
                    {
                        Id = "Id5",
                        Name = "Name5",
                        Path = "Path5"
                    },
                new MediaFolder
                    {
                        Id = "Id6",
                        Name = "Name6",
                        Path = "Path6"
                    }
            };

        var target = new List<MediaFolder>
            {
                new MediaFolder
                    {
                        Id = "Id1",
                        Name = "Actualizado en el objeto",
                        Path = "Path1"
                    },
                    //Id2 eliminado
                new MediaFolder
                    {
                        Id = "Id3",
                        Name = "Name3",
                        Path = "Actualizado tambien"
                    },
                new MediaFolder
                    {
                        Id = "Id4",
                        Name = "Name4",
                        Path = "Path4"
                    },
                new MediaFolder
                    {
                        Id = "Id5",
                        Name = "Name5",
                        Path = "Path5"
                    },
                new MediaFolder
                    {
                        Id = "Id6",
                        Name = "Name6",
                        Path = "Path6"
                    },
                     new MediaFolder
                    {
                        Id = "Id7",
                        Name = "Nuevo Item 7",
                        Path = "Nuevo Item 7"
                    }
            };

        var merge = source.Merge(target, (x, y) => x.Id == y.Id);

        var toUpdate = merge.Matched((x, y) => x.Name != y.Name | x.Path != y.Path)
            .ToArray();

        var toDelete = merge.NotMatchedBySource();
        var toInsert = merge.NotMatchedByTarget();

        Assert.AreEqual(2, toUpdate.Count());
        Assert.IsTrue(toUpdate.Count(x => x.Source.Id == "Id1" & x.Target.Id == "Id1") > 0);
        Assert.IsTrue(toUpdate.Count(x => x.Source.Id == "Id3" & x.Target.Id == "Id3") > 0);

        Assert.AreEqual("Id7", toInsert.First().Id);
        Assert.AreEqual("Id2", toDelete.First().Id);
于 2013-07-23T23:32:07.230 回答