2014-02-25 90 views
5

我怎樣才能trasfrom下面的SQL語句轉換成LINQ選擇情況下,LINQ在C#

select AdsProperties.AdsProID, 
     AdsProperties.IsActive, 
     AdsProperties.Name, 
      case 
       when AdsProperties.ControlType =1 then -- TextBox 
        'Textbox' 
       when AdsProperties.ControlType =2 then -- DropDown 
        'Dropdown' 
       when AdsProperties.ControlType =3 then -- ConboBox 
        'ComboBox' 
       else -- RadioButton 
        'RadioButtont' 
      end as ControlType 
    from CLF.utblCLFAdsPropertiesMaster as AdsProperties 

我已經試過這

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters 
      select new 
      { 
       AdsProperties.AdsProID, 
       AdsProperties.Name, 
       AdsProperties.IsActive, 
       ControlType = AdsProperties.ControlType == 1 ? (int?)"TextBox" : null, 
       ControlType = AdsProperties.ControlType == 2 ? (int?)"Dropdown" : null, 
       ControlType = AdsProperties.ControlType == 3 ? (int?)"ComboBox" : null,     
       ControlType = AdsProperties.ControlType == 4 ? (int?)"RadioButton" : null) 
      }; 
      dt = query.CopyToDataTableExt(); 

但我得到這個錯誤

`an anynomous type cannot have multiple properties with the same name` 

我知道這可能很簡單。然而,作爲linq的新人,我沒有相應的經驗來處理它。任何幫助,將不勝感激。提前致謝。

+1

您可以爲switch語句使用單獨的方法,並將其分配給ControlType屬性。 'ControlType = GetControlType(AdsProperties.ControlType)' –

+1

爲什麼不使用Enum來代替int的ControlType? – Alex

+0

魔法串是邪惡的。 – Aron

回答

4

聲明字符串數組爲:

string[] controls = new string[] {"TextBox","Dropdown","ComboBox","RadioButton"}; 

修改您的查詢如下所述:

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters 
        select new 
        { 
         AdsProperties.AdsProID, 
         AdsProperties.Name, 
         AdsProperties.IsActive, 
         ControlType = AdsProperties.ControlType < controls.Length ? controls[AdsProperties.ControlType-1] : null 
        }; 
     dt = query.CopyToDataTableExt(); 
+0

嗯,我不認爲這是最簡單的方法來做到這一點。如果添加了索引爲999的另一種控件類型會怎麼樣?另外,controlType可以有不同的類型,如果它是可以爲空的呢?甚至更改爲字符串值。我認爲這是一種解決方法,而不是一種解決方案,因爲如果將來系統發生變化,這肯定會失敗。 –

+0

@ShaktiPrakashSingh:謝謝你的建議。你可以現在回顧我的答案嗎? –

1
var query = from AdsProperties in db.utblCLFAdsPropertiesMasters 
        select new 
        { 
         AdsProperties.AdsProID, 
         AdsProperties.Name, 
         AdsProperties.IsActive, 
         ControlType = (AdsProperties.ControlType == 1) ? "TextBox" : 
             (AdsProperties.ControlType == 2) ? "Dropdown" : 
             (AdsProperties.ControlType == 3) ? "ComboBox" : 
             (AdsProperties.ControlType == 4) ? "RadioButton" : "" 
        }; 
     dt = query.CopyToDataTableExt(); 
+0

這與我發佈的內容有些相似。你擊敗了我8秒。 ;) –

+0

是的我在想,StackOver並沒有阻止它,似乎是服務器或其他地方的重大延遲... – Cynede

+0

反正,+1擊敗我的時間。 ;) –

1

您可以使用下面的代碼。

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters 
    select new 
    { 
     AdsProperties.AdsProID, 
     AdsProperties.Name, 
     AdsProperties.IsActive, 
     ControlType = AdsProperties.ControlType == 1 ? "TextBox" : 
      (AdsProperties.ControlType == 2) ? "Dropdown" : 
      (AdsProperties.ControlType == 3) ? "ComboBox" : 
      (AdsProperties.ControlType == 4) ? "RadioButton" : 
      null) 
    }; 
dt = query.CopyToDataTableExt(); 

另外,如果你想使它不那麼亂,你可以嘗試把你的病情的另一種方法,並作出這樣一個電話:

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters 
    select new 
    { 
     AdsProperties.AdsProID, 
     AdsProperties.Name, 
     AdsProperties.IsActive, 
     ControlType = GetControlType(AdsProperties.ControlType) 
    }; 
dt = query.CopyToDataTableExt(); 

而且你GetControlType會是這樣的:

private string GetControlType(int controlIndexOrWhatever) 
{ 
    switch(controlIndexOrWhatever) 
    { 
     case 1: return "TextBox"; 
     case 2: return "DropDown"; 
     case 3: return "ComboBox"; 
     case 4: return "RadioButton"; 
     default: return null;    
    } 
} 

但是,然後再次,它沒有測試,並不適用於實體的LINQ。我認爲它可能適用於LINQ to SQL。不介意語法問題(如果有的話),只寫代碼而不檢查VS.

+0

動態類型轉換字符串爲空,請解釋你期望的是什麼? – Cynede

+0

我的不好,只是複製了OPs代碼。我不確定他的意思,但提供了一些解決方案。 –

+1

不適用於LinqToSQL。它不是一個表達。 – Aron