简体   繁体   中英

Assign Values from one list to Another list based on Id

Could you please help me in Assigning values to a List 2 from another List 1 based on a id, Here are the two Lists structure which are the object of two classes :

List 1 object :

public class Person
{
   int id;
   string fnName;
   string lnName;
}

List 2 Object:

public class Payment
{
   int paymentId;
   string paymentType;
   IEnumerable<string> paymentPersonIds;
   IEnumerable<Person> PersonDetails;

}

Basically I get the data for List 1 from a one function and List 2 data from other function, I need to fill the Person Details in Payment List if PaymentPersonIds matches in List 1 id?

Maybe something like this:

list1.Where(c => payment.paymentPersonIds.ToList().Contains(c.id.ToString()));

Also I would suggest to make some modification in your code, for instance you need to consider the following as a property:

public class Person
{
    public int Id { get; set; }
    public string FnName { get; set; }
    public string LnName { get; set; }
}

https://dotnetfiddle.net/qJ8Kf8

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var List1 = new List<Person>{
            new  Person{
                id  =  1,
                fnName  = "bla"
            },  new  Person{
                id  =  2,
                fnName  = "bla2"
            }
        };

            var List2 = new List<Payment>{
            new  Payment{
                paymentId  =  1,
                paymentType  = "type1",
                paymentPersonIds  =  new string[]{"1"},
                PersonDetails = Enumerable.Empty<Person>(),
            },
            new  Payment{
                paymentId  =  2,
                paymentType  = "type2",
                paymentPersonIds  =  new string[]{"1","2"},
                PersonDetails = Enumerable.Empty<Person>(),
            }
        };

            List2.ForEach(l2 => {
                var personList = new List<Person>();
                l2.paymentPersonIds.ToList().ForEach(id => {
                    var matched = List1.FirstOrDefault(l1 => l1.id.ToString().Contains(id));
                    if (matched != null)
                    {
                        personList.Add(matched);
                    }
                });
                l2.PersonDetails = personList as IEnumerable<Person>;
            });
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(List2));
        }
    }
    public class Person
    {
        public int id;
        public string fnName;
        public string lnName;
    }

    public class Payment
    {
        public int paymentId;
        public string paymentType;
        public IEnumerable<string> paymentPersonIds;
        public IEnumerable<Person> PersonDetails;
    }
}

As other answers says, you should use properties instead of fields.

I'd use linq for this

foreach (Payment payment in payments)
{
   payment.PersonDetails = persons.Where(p =>  payment.PaymentPersonIds.Contains(p.ToString()));
}

Here is the full code:

public class Person
{
    public int Id { get; set; }
    public string FnName { get; set; }
    public string LnName { get; set; }
}


public class Payment
{
    public int PaymentId { get; set; }
    public string PaymentType { get; set; }
    public IEnumerable<string> PaymentPersonIds { get; set; }
    public IEnumerable<Person> PersonDetails { get; set; }

    /// <summary> FIll PersonDetails property using Linq </summary>
    /// <param name="payments">List of payments</param>
    /// <param name="persons">List of persons</param>
    public static void FillPersonDetailsLinq(List<Payment> payments, List<Person> persons)
    {            
        foreach (Payment payment in payments)
        {
           payment.PersonDetails = persons.Where(p => payment.PaymentPersonIds.Contains(p.Id.ToString()));
        }           
    }

    /// <summary> FIll PersonDetails property without using Linq </summary>
    /// <param name="payments">List of payments</param>
    /// <param name="persons">List of persons</param>
    public static void FillPersonDetails(List<Payment> payments, List<Person> persons)
    {
        foreach (Payment payment in payments)
        {
            List<Person> matches = new List<Person>();
            foreach (Person person in persons)
            {
                if (payment.PaymentPersonIds.Contains(person.Id.ToString()))
                {
                    matches.Add(person);
                }
            }
            payment.PersonDetails = matches;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM