2017-12-20 9 views
1

これらは私の二つの表のとおりです。列のQuery- LINQ、2つのテーブル、連合

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

parkingLot(parkingLotID, address, latitude, longtitude, status, 
      PricePerHour, maxSpaces, occupiedSpaces, freeSpaces, isOrdered) 

すべてのID以外は同じです。 status==trueで -

私は利用可能なすべての车场(publicParking/parkingLot)との価格が注文したテーブルを返しますLINQでクエリを記述する必要があります。

表は次のようになります。私は労働組合を行う必要があります

IDアドレス緯度経度のステータス

、または私は最初の列だけでIDを呼び出しますので、テーブルを変更する必要がありますか?私はLINQPad5ととして働いています

ERROR

(代わりにpublicParkingIDparkingLotIDの)それは、このエラーを与える私はこのコードを試してみたが、それは

var union = 
     (from lot in parkingLots 
     where lot.status == true 
     select lot).Union(from pub in publicParkings 
     where pub.status==true 
     select pub); 

を動作しません。 tutorialsteacherのコードエディタ他にもオプションがありますか?

答えて

4

Unionを使用するには、両方の結果シーケンスに同じタイプが含まれている必要があります。あなたの例では、外側のクエリはparkingLotと内側のpublicParkingを含んでいます。

それは匿名型を使用して解決することができます。さらに、データ処理のための

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}); 
+0

ありがとうございました!ありがとうございました!ありがとうございました!ありがとうございました!ありがとうございました!ありがとうございました! –

関連する問題