如何将下面的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
我试过了
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 的新手,我没有适当的经验来处理它。任何帮助,将不胜感激。提前致谢。
最佳答案
将字符串数组声明为:
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();
关于c# - 在 C# 的 linq 中选择大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22010655/