На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
msm.ru
  
> Как организовать LINQ Left Join с тремя таблицами? , Как сделать с двумя понятно
    Вот рабочий пример, как организовать Left Join для одной таблицы (City).

    Эквивалент sql запроса:

    ExpandedWrap disabled
      select
        cl.Name as clientName,
        ci.Name as cityName
      from Client cl
        left join City ci on ci.ID = cl.CityID


    ExpandedWrap disabled
      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 запроса:

    ExpandedWrap disabled
      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
      Всем спасибо, решение найдено.
      0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
      0 пользователей:


      Рейтинг@Mail.ru
      [ Script execution time: 0,0174 ]   [ 16 queries used ]   [ Generated: 25.04.24, 11:56 GMT ]