Объединить два списка List - C#
Формулировка задачи:
Есть класс Client(список покупателей) и класс Checkout(список покупок). Необходимо сделать так, чтобы при поиске клиента выводилась информация о нём и о его покупках.
Вот код:
У меня вышло так: при вводе допустим "25" выводит Шульшенко Лилия Васильевна Купил тетрадь(кол-во 3)
Нужно наверное объединить эти два списка по полю Id с сохранением всех заполненных полей, чтобы при вводе допустим имени "Лилия", выводило всё это.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
List<Client> lst = new List<Client>();
lst.Add(new Client { Id = 23, Familiya = "Шульженко", Imya = "Олег", Otchestvo = "Николаевич" });
lst.Add(new Client { Id = 25, Familiya = "Шульженко", Imya = "Лилия", Otchestvo = "Васильевна" });
lst.ForEach(Console.WriteLine);
List<Checkout> lst1 = new List<Checkout>();
lst1.Add(new Checkout { Id = 23, buy = "Диск(кол-во 1)" });
lst1.Add(new Checkout { Id = 25, buy = "Тетрадь(кол-во 3)" });
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nПоиск по Ф/И/О:");
Console.ResetColor();
string str = Console.ReadLine();
var result = lst.Select(x => String.Join(" ", x.ToString())).Where(y => y.Contains(str));
var result1 = lst1.Select(x => String.Join(" ", x.ToString())).Where(y => y.Contains(str));
foreach (var item in result)
Console.WriteLine(item);
foreach (var item1 in result1)
Console.WriteLine(item1);
Console.Read();
}
}
public class Client
{
public int Id { get; set; }
public string Familiya { get; set; }
public string Imya { get; set; }
public string Otchestvo { get; set; }
public string Checkout { get; set; }
public int Price { get; set; }
public override string ToString()
{
return "Человек " + Id + " " + Familiya + " " + Imya + " " + Otchestvo + " " + Checkout;
}
}
public class Checkout : Client
{
public string buy { get; set; }
public override string ToString()
{
Id = Id;
Imya += buy;
return "Купил " + Id + " " + buy;
}
}
}Решение задачи: «Объединить два списка List»
textual
Листинг программы
List<Client> lst = new List<Client>();
lst.Add(new Client { Id = 23, Familiya = "Шульженко", Imya = "Олег", Otchestvo = "Николаевич" });
lst.Add(new Client { Id = 25, Familiya = "Шульженко", Imya = "Лилия", Otchestvo = "Васильевна" });
lst.ForEach(Console.WriteLine);
List<Checkout> lst1 = new List<Checkout>();
lst1.Add(new Checkout { Id = 23, buy = "Диск(кол-во 1)" });
lst1.Add(new Checkout { Id = 25, buy = "Тетрадь(кол-во 3)" });
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nПоиск по Ф/И/О:");
Console.ResetColor();
string str = Console.ReadLine();
var result = (from cust in lst
join check in lst1 on cust.Id equals check.Id
select new
{
CustomerId = cust.Id,
Info = string.Format("{0} {1} {2} купил {3}", cust.Familiya, cust.Imya, cust.Otchestvo, check.buy)
}).Where(r => r.Info.Contains(str));
foreach (var r in result)
Console.WriteLine(r.Info);
Console.ReadLine();