2011-06-16 49 views
1

我有5张桌子下面如何遵循:如何选择价格大于“0”的多列单列?

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做到为sql?

非常感谢您的关注。

玩得开心。

干杯

对不起,我修改了一个问题:因为我忘了写下来另一个表(HotelRoom_TBL),所以请原谅我这个错误。 感谢这么多

回答

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

感谢这么多mr.Marc的代码是正确的,但我忘了插入另一台所以请尽量同情我的错误。 – JayJay 2011-06-16 13:46:59

+0

@JayJay所以,内心加入酒店... – 2011-06-16 13:48:39

+0

是的马克,这是非常好的作品...非常感谢你的支持,祝你今天过得愉快。 – JayJay 2011-06-16 14:14:46