查询 - 数据集中的全外连接2个不同的表 - LINQ C#

时间:2017-12-19 12:44:35

标签: c# sql asp.net linq datatables

我想从两个不同的表进行连接(它们之间没有连接):

停车场(parkingLotID,addressParkingLot,statusParkingLot)

PublicParking (publicParkingID,addressPublicParking,statusParking)。

我想编写一个返回所有可用停车位的查询 -  根据他们的状态(停车场和公共停车场)。

我已经读过我需要进行全外连接(制作一个大表),然后才能编写查询。

我需要在LINQ中编写查询。

我真的需要你对这个查询和全外连接的帮助(如果它是正确的)

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

public class Program
{
    public static void Main()
    {

        IList<parkingLot> parkingLot=new List <parkingLot>(){

        new parkingLot {parkingLotID=1, addressParkingLot="bograshov 22",statusParkingLot=true},
        new parkingLot {parkingLotID=2, addressParkingLot="bograshov 10",statusParkingLot=false},
        new parkingLot {parkingLotID=3, addressParkingLot="bograshov 28",statusParkingLot=true},
    };



    IList<publicParking> PublicParking=new List <publicParking>(){

        new publicParking {publicParkingID=101, addressPublicParking= "bograshov 23",statusParking=true},
        new publicParking {publicParkingID=102, addressPublicParking= "bograshov 21",statusParking=true},
        new publicParking {publicParkingID=103, addressPublicParking= "bograshov 18",statusParking=false},
    };


  (from lot in parkingLot
    where lot.statusParkingLot == true
    select lot).Union(from pub in PublicParking
    where pub.statusParking==true
    select pub);


   }
}

public class publicParking 
{
 public int publicParkingID { get; set; }
 public string addressPublicParking { get; set; }
    public bool statusParking { get; set; }

}


public class parkingLot 
{
  public int parkingLotID { get; set; }
  public string addressParkingLot { get; set; }
  public bool statusParkingLot { get; set; }

 }

TNX!

更新

我编写了查询,但它有一个问题:

New problem

1 个答案:

答案 0 :(得分:1)

您可以使用Union连接两个表,而无需使用公共字段。您的场景的LINQ查询将如下所示。

 (from lot in ParkingLots
 where lot.StatusParkingLot == true
 select lot).Union( from pub in PublicParkings
 where pub.StatusParking==true
 select pub);

希望这有效!