c# - 在 C# 的 linq 中选择大小写

标签 c# linq

如何将下面的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/

相关文章:

c# - VB.NET 相当于这个 C# linq 类

linq - 如何使用 FLinq 在 F# 中进行外连接?

c# - Mongo C# Driver 2.0 聚合组异常

javascript - 从 @foreach 循环内部访问外部变量

c# - protobuf-net 重复字段作为 ObservableCollection<T>

c# - 如何在 Linux 上的 .net Core 中获取系统规范

c# - SignalR LongPolling multiple Groups.Add for a single client Exception

c# - SomeButNotAll() 是否有优雅的 LINQ 解决方案?

c# - Controller 未收到英国格式的 MVC3 日期

c# - Convert.ToString 方法和 Object.ToString() 在全局化方面的区别