Combine two lists in C #

I want to combine two lists with different attributes into one list, but when merging it, I want to check if this particular example has an exact date that is in both lists, and if so, I want to take both attributes from these elements and combine them into one item in another list

List 1:

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

Result L1:

enter image description here

List 2:

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

Result L2:

enter image description here


Required Result:

enter image description here

This is currently how I merge r1 and r2:

 List<object> r3 = new List<object>(r1.Concat(r2));
+4
source share
4 answers

You can convert them to one type and use things like this.

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);
+2
source

Create dictionary

Dictionary MyDict<String, List<Object>>;

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

, " " , .

, O (n)

, , null IE

MyDict[Object.Period] != null

, Nikhil Agrawal , Object ... . , ().

0

AFAK , , , ,

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

Nikhil Agrawal , , , , .

Ignoring this and accepting it as a call (use the anonymous types passed to the object), this is what I came up with:

The code that integrates contains the full outer join:

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

and the required comparator is below:

    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();
            ;
        }
    }
0
source

Source: https://habr.com/ru/post/1547727/


All Articles