我怎样才能将以下sql语句转换为linq
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
Run Code Online (Sandbox Code Playgroud)
我试过这个
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();
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误
`an anynomous type cannot have multiple properties with the same name`
Run Code Online (Sandbox Code Playgroud)
我知道这可能很简单.然而,作为linq的新人,我没有处理它的approrpiate经验.任何帮助,将不胜感激.提前致谢.
将字符串数组声明为:
string[] controls = new string[] {"TextBox","Dropdown","ComboBox","RadioButton"};
Run Code Online (Sandbox Code Playgroud)
如下所述修改您的查询:
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();
Run Code Online (Sandbox Code Playgroud)