c# - 我如何从另一个可枚举的 c# 中插入一个可枚举的

标签 c# linq merge ienumerable

我浏览了该站点上的问题,但没有找到与我的特定问题相匹配的问题。

假设我有以下内容:

Product[] store1 = { new Product { Name = "apple", Code = 9, Code1="1" }, 
                   new Product { Name = "orange", Code = 4 } };

Product[] store2 = { new Product { Name = "apple", Code = 9, Code2="2" }, 
                   new Product { Name = "lemon", Code = 12 } };

与:

public class Product : IEquatable<Product>
{
    public string Name { get; set; }
    public int Code { get; set; }
    public string Code1 { get; set; }
    public string Code2 { get; set; }

    public bool Equals(Product other)
    {

        //Check whether the compared object is null. 
        if (Object.ReferenceEquals(other, null)) return false;

        //Check whether the compared object references the same data. 
        if (Object.ReferenceEquals(this, other)) return true;

        //Check whether the products' properties are equal. 
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public override int GetHashCode()
    {

        //Get hash code for the Name field if it is not null. 
        int hashProductName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = Code.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }
}

如何返回一个单一的 Enumerable,其中 store1 的数据在匹配时被 store2 的数据覆盖,并且在非匹配时刚从 store2 插入到 store1 中。基本上,我正在寻找与 TSQL Merge 语句等效的 C#。

在一天结束时运行这个:

foreach (var product in union)
      Console.WriteLine(product.Name + " " + product.Code + " " + product.Code1 + " " + product.Code2);

我想回去:

苹果 9 1 2

橙色 4

柠檬 12

但是当我运行这个时:

IEnumerable<Product> union = store1.Union(store2);

我得到:

苹果 9 1

橙色 4

柠檬 12

当我运行这个时:

IEnumerable<Product> union = store1.Concat(store2);

我得到:

苹果 9 1

橙色 4

苹果 9 2

柠檬 12

在此先感谢您的帮助。

最佳答案

        //
        // Summary:
        //     Produces the set union of two sequences by using the default equality comparer.
        //
        // Parameters:
        //   first:
        //     An System.Collections.Generic.IEnumerable<T> whose distinct elements form
        //     the first set for the union.
        //
        //   second:
        //     An System.Collections.Generic.IEnumerable<T> whose distinct elements form
        //     the second set for the union.
        //
        // Type parameters:
        //   TSource:
        //     The type of the elements of the input sequences.
        //
        // Returns:
        //     An System.Collections.Generic.IEnumerable<T> that contains the elements from
        //     both input sequences, excluding duplicates.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     first or second is null.
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);

因为函数的原因

excluding duplicates.

所以你必须为 Product[] 编写联合函数

public static class ProductExtension
{
    public static IEnumerable<T> Union<T>(this IEnumerable<T> store1, IEnumerable<T> store2)
    {
        List<T> List = new List<T>();
        foreach (var item in store2)
        {
            if (store1.Any(n=>n.Equals(item)))
            {
                var obj = store1.First(n => n.Equals(item));
                foreach (System.Reflection.PropertyInfo pi in obj.GetType().GetProperties())
                {
                    object v1 = pi.GetValue(obj, null);
                    object v2 = pi.GetValue(item, null);
                    var value = v1;
                    if (v2 != null && (v1 == null || v1.ToString() == string.Empty) && v1 != v2)
                    {
                        value = v2;
                    }
                    pi.SetValue(obj, value, null);
                }
                List.Add(obj);
            }
            else {
                List.Add(item);
            }            
        }

        foreach (var item in store1) { 
            if(!store2.Any(n=>n.Equals(item))){
                List.Add(item);
            }
        }
        return List.AsEnumerable();
    }
}

关于c# - 我如何从另一个可枚举的 c# 中插入一个可枚举的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095895/

相关文章:

c# - 我可以使用 linq 的复数化术语的方法吗?

c# - 使用linq查询时如何获取int数组的索引

mysql - 如何合并两个具有相同结构的mysql表

c# - MOSS Filter webpart 不适用于查询字符串中的 & 符号

c# - 以编程方式将天 (1-31) 绑定(bind)到下拉列表,并将 0 绑定(bind)到天 < 10?

c# - 在 Expression<TDelegate> 中为 TDelegate 注入(inject)参数值并减少表达式

python - 基于部分startswith匹配合并两个数据帧

c# - 使用 Process 类检查控制台应用程序是否仍在运行

c# - 我已经在 MySQL Workbench 中创建了 MySQL 数据库,但无法在 C# 中使用

java - 合并两个 flv 视频的最简单方法是什么?