Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.236.100.210] |
|
Сообщ.
#1
,
|
|
|
Вот рабочий пример, как организовать Left Join для одной таблицы (City).
Эквивалент sql запроса: select cl.Name as clientName, ci.Name as cityName from Client cl left join City ci on ci.ID = cl.CityID using System; using System.Collections.Generic; using System.Linq; namespace TestLinq { class Program { public class Client { public int ID; public string Name; public int ?CityID; } public class City { public int ID; public string Name; public int ?RegionID; } public class Region { public int ID; public string Name; } public static List<Client> GetClients() { return new List<Client>() { new Client {ID = 1, Name = "Client A", CityID = 1 }, new Client {ID = 2, Name = "Client B", CityID = 2 }, new Client {ID = 3, Name = "Client C", CityID = null }, new Client {ID = 4, Name = "Client D", CityID = 2 } }; } public static List<City> GetCities() { return new List<City>() { new City {ID = 1, Name = "City A", RegionID = 1 }, new City {ID = 2, Name = "City B", RegionID = 3 }, new City {ID = 3, Name = "City C", RegionID = null }, new City {ID = 4, Name = "City D", RegionID = 3 } }; } public static List<Region> GetRegions() { return new List<Region>() { new Region {ID = 1, Name = "Region A"}, new Region {ID = 2, Name = "Region B"}, new Region {ID = 3, Name = "Region C"}, new Region {ID = 4, Name = "Region D"} }; } static void Main(string[] args) { var result = from clients in GetClients() join cities in GetCities() on clients.CityID equals cities.ID into clientCityGroup from subCities in clientCityGroup.DefaultIfEmpty() select new { clientName = clients.Name, cityName = subCities?.Name }; foreach (var current in result) Console.WriteLine($"{current.clientName} из города {current?.cityName}"); Console.ReadKey(); } } } Вопрос: как организовать Left Join в LINQ для двух таблиц (City, Region)? Эквивалент sql запроса: select cl.Name as clientName, ci.Name as cityName, rg.Name as regionName from Client cl left join City ci on ci.ID = cl.CityID left join Region rg on rg.ID = ci.RegionID |
Сообщ.
#2
,
|
|
|
Всем спасибо, решение найдено.
|