2010-08-30 42 views
0

是否有可能做一個包含if的查詢?我想要做的是根據同一個表中另一列的值選擇兩個特定的列。linq查詢使用if

所以如果column10,我想選擇custom1custom2,如果它是1,我想選擇custom3custom4

+0

的可能重複【如何做如果LINQ查詢語句(http://stackoverflow.com/questions/1153486/how-to-do-if-statement-in-linq-query) – 2010-08-30 19:49:21

+0

那一個看起來很複雜,但我想你是對的。我在「尋找」的答案是可以在linq中做條件運算符。 – peirix 2010-08-31 05:58:26

回答

1
var q = from c from MyTable 
     select new 
     { 
      item1 = (c.column1 == 0) ? custom1 : custom3, 
      item2 = (c.column1 == 0) ? custom2 : custom4 
     }