簡體   English   中英

在C#中選擇linq中的case

[英]select case in linq in C#

我怎樣才能將以下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的新人,我沒有處理它的approrpiate經驗。 任何幫助,將不勝感激。 提前致謝。

將字符串數組聲明為:

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();
  var query = from AdsProperties in db.utblCLFAdsPropertiesMasters
                    select new
                    {
                        AdsProperties.AdsProID,
                        AdsProperties.Name,
                        AdsProperties.IsActive,
                         ControlType = (AdsProperties.ControlType == 1) ? "TextBox" :
                                       (AdsProperties.ControlType == 2) ? "Dropdown" :
                                       (AdsProperties.ControlType == 3) ? "ComboBox" : 
                                       (AdsProperties.ControlType == 4) ? "RadioButton" : ""
                    };
        dt = query.CopyToDataTableExt();

您可以使用以下代碼。

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters
    select new
    {
        AdsProperties.AdsProID,
        AdsProperties.Name,
        AdsProperties.IsActive,
        ControlType = AdsProperties.ControlType == 1 ? "TextBox" : 
            (AdsProperties.ControlType == 2) ? "Dropdown" : 
            (AdsProperties.ControlType == 3) ? "ComboBox" : 
            (AdsProperties.ControlType == 4) ? "RadioButton" : 
            null)
    };
dt = query.CopyToDataTableExt();

此外,如果你想讓它變得不那么混亂,你可以嘗試將你的條件放在另一種方法中並進行如下調用:

var query = from AdsProperties in db.utblCLFAdsPropertiesMasters
    select new
    {
        AdsProperties.AdsProID,
        AdsProperties.Name,
        AdsProperties.IsActive,
        ControlType = GetControlType(AdsProperties.ControlType)
    };
dt = query.CopyToDataTableExt();

而你的GetControlType將是這樣的:

private string GetControlType(int controlIndexOrWhatever)
{
    switch(controlIndexOrWhatever)
    {
        case 1: return "TextBox";
        case 2: return "DropDown";
        case 3: return "ComboBox";
        case 4: return "RadioButton";
        default: return null;            
    }
}

但話說回來,它沒有經過測試,也無法與LINQ一起使用。 我認為它可能適用於LINQ to SQL。 不介意語法問題,如果有的話,只需編寫代碼而不檢查VS.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM