select case in linq in C#
c#, linq
Solution
Declare an array of strings as:
string[] controls = new string[] {"TextBox","Dropdown","ComboBox","RadioButton"};
Modify your query as mentioned below:
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();
Problem
How can I trasfrom the following sql statement into 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 ``` I have tried this ``` 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(); ``` but I am getting this error ``` `an anynomous type cannot have multiple properties with the same name` ``` I know that it may be easy and simple. However, being new in linq, I haven't the approrpiate experience to deal with it. Any help would be appreciated. Thanks in advance.