C# 查询-LINQ,2个表,Union

C# 查询-LINQ,2个表,Union,c#,sql,asp.net,linq,C#,Sql,Asp.net,Linq,这是我的两张桌子: publicParking(publicParkingID, address, latitude, longtitude, status, PricePerHour, maxSpaces, occupiedSpaces, freeSpaces, isOrdered) 及 除ID外,所有列都相同 我需要在LINQ中编写查询,该查询将返回一个按价格排序的表,其中包含所有可用的停车场(publicParking/parkingLot)-withstat

这是我的两张桌子:

publicParking(publicParkingID, address, latitude, longtitude, status,
              PricePerHour, maxSpaces, occupiedSpaces, freeSpaces, isOrdered)

除ID外,所有列都相同

我需要在LINQ中编写查询,该查询将返回一个按价格排序的表,其中包含所有可用的停车场(
publicParking
/
parkingLot
)-with
status==true

该表应如下所示:

ID地址经纬度状态

我应该进行联合,还是应该更改表以便第一列只调用ID?(而不是
publicParkingID
parkingLotID

我已经尝试过这个代码,但它不起作用

var union =  
         (from lot in parkingLots
         where lot.status == true
         select lot).Union( from pub in publicParkings
         where pub.status==true
         select pub);
它给出了以下错误:


我正在使用LINQPad5和tutorialsteacher的代码编辑器。还有其他选项吗?

要使用
联合
两个结果序列必须包含相同的类型。在您的示例中,外部查询包含
停车场
和内部
公共停车场

可以使用匿名类型解决此问题:

var union =  
     (from lot in parkingLots
     where lot.status == true
     orderby lot.PricePerHour // don't forget ordering
     select new {
           ID = lot.parkingLotID, 
           lot.address, lot.latitude, lot.longitude, lot.status})
    .Union(from pub in publicParkings
     where pub.status==true
     orderby pub.PricePerHour // don't forget ordering
     select new {
           ID = pub.publicParkingID, 
           pub.address, pub.latitude, pub.longitude, pub.status});
但对于进一步的数据处理来说,定制类可能更好:

public class ParkingData
{
    public int ID {get; set;}
    public string Address {get; set;}
    public double Latitude {get; set;}
    public string Longitude {get; set;}
    public bool Status {get; set;}
}
然后像这样询问:

var union =  
     (from lot in parkingLots
     where lot.status == true
     orderby lot.PricePerHour // don't forget ordering
     select new ParkingData {
           ID = lot.parkingLotID, 
           Address = lot.address, 
           Latitude = lot.latitude, 
           Longitude = lot.longitude, 
           Status = lot.status})
    .Union(from pub in publicParkings
     where pub.status==true
     orderby pub.PricePerHour // don't forget ordering
     select new {
     select new ParkingData {
           ID = pub.publicParkingID, 
           Address = pub.address, 
           Latitude = pub.latitude, 
           Longitude = pub.longitude, 
           Status = pub.status});

要使用Union,两个结果序列必须包含相同的类型。在您的示例中,外部查询包含
停车场
和内部
公共停车场

可以使用匿名类型解决此问题:

var union =  
     (from lot in parkingLots
     where lot.status == true
     orderby lot.PricePerHour // don't forget ordering
     select new {
           ID = lot.parkingLotID, 
           lot.address, lot.latitude, lot.longitude, lot.status})
    .Union(from pub in publicParkings
     where pub.status==true
     orderby pub.PricePerHour // don't forget ordering
     select new {
           ID = pub.publicParkingID, 
           pub.address, pub.latitude, pub.longitude, pub.status});
但对于进一步的数据处理来说,定制类可能更好:

public class ParkingData
{
    public int ID {get; set;}
    public string Address {get; set;}
    public double Latitude {get; set;}
    public string Longitude {get; set;}
    public bool Status {get; set;}
}
然后像这样询问:

var union =  
     (from lot in parkingLots
     where lot.status == true
     orderby lot.PricePerHour // don't forget ordering
     select new ParkingData {
           ID = lot.parkingLotID, 
           Address = lot.address, 
           Latitude = lot.latitude, 
           Longitude = lot.longitude, 
           Status = lot.status})
    .Union(from pub in publicParkings
     where pub.status==true
     orderby pub.PricePerHour // don't forget ordering
     select new {
     select new ParkingData {
           ID = pub.publicParkingID, 
           Address = pub.address, 
           Latitude = pub.latitude, 
           Longitude = pub.longitude, 
           Status = pub.status});

非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。非常感谢。