Union error
Hey!
I am trying to the union between 2 different tables and it give me an error
This is the error:
I am trying to the union between 2 different tables and it give me an error
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var parkingLots=new List <parkingLot>{
new parkingLot() {parkingLotID=1, addressParkingLot="bograshov 22",latitudeParkingLot=32.076994, longtitudeParkingLot=34.769416, maxSpaces=15, occupiedSpaces=7,freeSpaces=8,PricePerHour=60,PricePerMinute=1,statusParkingLot=true},
new parkingLot() {parkingLotID=2, addressParkingLot="bograshov 10",latitudeParkingLot=32.077428, longtitudeParkingLot=34.768551, maxSpaces=25, occupiedSpaces=5,freeSpaces=20,PricePerHour=30,PricePerMinute=0.5,statusParkingLot=false},
new parkingLot() {parkingLotID=3, addressParkingLot="bograshov 28",latitudeParkingLot=32.076806, longtitudeParkingLot=34.769922, maxSpaces=12, occupiedSpaces=10,freeSpaces=2,PricePerHour=36,PricePerMinute=0.6,statusParkingLot=true},
};
var publicParkings= new List <publicParking>{
new publicParking {publicParkingID=101, addressPublicParking= "bograshov 23", latitude=32.077055, longtitude=34.769837,statusParking=true, PricePerHourpublicParking=30, PricePerMinutepublicParking=0.5, isOrdered=false},
new publicParking {publicParkingID=102, addressPublicParking= "bograshov 21", latitude=32.076962, longtitude=34.769584,statusParking=true, PricePerHourpublicParking=180, PricePerMinutepublicParking=3, isOrdered=false},
new publicParking {publicParkingID=103, addressPublicParking= "bograshov 18", latitude=32.077144, longtitude=34.76911, statusParking=false,PricePerHourpublicParking=6, PricePerMinutepublicParking=0.1, isOrdered=false},
};
/* var query = from p in parkingLots
where p.statusParkingLot==false
select p;
query.ToList().ForEach(p => Console.WriteLine(p.addressParkingLot));
*/
var query = (from lot in parkingLots where lot.statusParkingLot == false select lot).Union(from pub in publicParkings where pub.statusParking==true select pub);
query.ToList().ForEach(lot => Console.WriteLine(lot .addressParkingLot));
query.ToList().ForEach(pub=> Console.WriteLine(pub.addressPublicParking));
}
}
public class publicParking
{
public int publicParkingID { get; set; }
public string addressPublicParking { get; set; }
public double latitude { get; set; }
public double longtitude { get; set; }
public bool statusParking { get; set; }
public int PricePerHourpublicParking { get; set; }
public double PricePerMinutepublicParking { get; set; }
public bool isOrdered { get; set; }
}
public class parkingLot
{
public int parkingLotID { get; set; }
public string addressParkingLot { get; set; }
public double latitudeParkingLot { get; set; }
public double longtitudeParkingLot { get; set; }
public int maxSpaces { get; set; }
public int occupiedSpaces { get; set; }
public int freeSpaces { get; set; }
public int PricePerHour { get; set; }
public double PricePerMinute { get; set; }
public bool statusParkingLot { get; set; }
}
This is the error:
Comments
var query = (from lot in parkingLots where lot.statusParkingLot == false select lot).Union(from pub in publicParkings where pub.statusParking==true select pub);
is invalid as you can't use Union to combine two different types. In this case you are trying to combine items of type publicParking with items of type parkingLot.
You can do something like
var query = (from lot in parkingLots where lot.statusParkingLot == false select lot.addressParkingLot).Union(from pub in publicParkings where pub.statusParking == true select pub.addressPublicParking);
because this is combining strings with strings .Then you can do
query.ToList().ForEach(r => Console.WriteLine(r));
which should give