简体   繁体   中英

Query - Full outer join 2 different tables in a Dataset - LINQ C#

I want to make a join from two different tables (with no connection between them):

Parkinglot (parkingLotID, addressParkingLot, statusParkingLot)

PublicParking (publicParkingID, addressPublicParking, statusParking).

And I want to write a query that returns all the parking that is available - based on their status (Parking lot & PublicParking).

I've read that I need to do full outer join (make one big table) and only then I can write the query.

I need to write the query in LINQ.

I really need your help about this query and about the full outer join (if it's right)

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!

UPDATE

I wrote the query but its have a problem:

New problem

You can use Union for joining two tables without having common fields. LINQ query for your scenario will look something like this.

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

Hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM