如何在多列中选择价格大于“0”的单列?

时间:2011-06-16 10:52:46

标签: c# linq

我有5个表格如下:

City_TBL

CityCode

   ABB
   DET
   FRI
   ROM

Hotel_TBL

HotelCode           CityCode (FK)      Price

    1                   ABB              0
    2                   ABB              10
    3                   FRI              0
    4                   DET              0
    5                   ROM              19

HotelRoom_TBL

RoomID         HotelCode (FK)      RoomName     Price

    1                   1          Superior       3
    2                   2          DeLuxe         6
    3                   1          Panoramic      0
    4                   3          Suite          0
    5                   4          Presidential   1

Transfer_TBL

TransferCode         CityCode (FK)      Price

    1                   ABB              3
    2                   ABB              6
    3                   DET              0
    4                   FRI              0

Cruise_TBL

CruiseCode         CityCode (FK)      Price

    1                   ABB              3
    2                   DET              0
    3                   FRI              0
    4                   ROM              0 

我想写下一个查询(linq),它将返回没有重复记录的CityCode列表(CityCode),其中至少有一个记录,其中字段/列“价格”(在表Hotel_TBL,Transfer_TBL,Cruise_TBL和HotelRoom_TBL)大于'0',如下图所示:

Result_TBL

   ABB
   ROM

如何使用linq to sql做到这一点?

非常感谢你的关注。

玩得开心。

干杯

抱歉,我修改了一个问题'我忘记写下另一张桌子(HotelRoom_TBL),所以请原谅我这个错误。 非常感谢

1 个答案:

答案 0 :(得分:2)

也许

var data = 
    ctx.HotelTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
    .Union(
     ctx.TransferTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
    ).Union(
     ctx.ResultTbl.Where(row => row.Price > 0).Select(row => row.CityCode)
    );

我个人觉得我很想使用ExecuteQuery:

var data = ctx.ExecuteQuery<string>(@"
select CityCode from Hotel_Tbl where Price > 0
union select CityCode from Transfer_Tbl where Price > 0
union select CityCode from Result_Tbl where Price > 0").ToList();