能帮我从另一个基于id的列表1向列表2赋值吗?这是两个列表结构的两个列表结构:
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;
}
基本上我是从一个功能中获得列表1的数据,而从其他功能中获得列表2的数据,如果PaymentPersonIds
与列表1 ID相匹配,我需要在付款列表中填写人员详细信息吗?
答案 0 :(得分:2)
也许是这样的:
list1.Where(c => payment.paymentPersonIds.ToList().Contains(c.id.ToString()));
我也建议您对代码进行一些修改,例如,您需要将以下内容视为属性:
public class Person
{
public int Id { get; set; }
public string FnName { get; set; }
public string LnName { get; set; }
}
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:0)
正如其他答案所述,您应该使用属性而不是字段。
我会为此使用linq
foreach (Payment payment in payments)
{
payment.PersonDetails = persons.Where(p => payment.PaymentPersonIds.Contains(p.ToString()));
}
这是完整的代码:
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;
}
}
}