我怎樣才能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的新人,我沒有相應的經驗來處理它。任何幫助,將不勝感激。提前致謝。
您可以爲switch語句使用單獨的方法,並將其分配給ControlType屬性。 'ControlType = GetControlType(AdsProperties.ControlType)' –
爲什麼不使用Enum來代替int的ControlType? – Alex
魔法串是邪惡的。 – Aron