久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從字符串映射枚舉

Mapping Enum from String(從字符串映射枚舉)
本文介紹了從字符串映射枚舉的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在數據庫表中有一個字符串列,它映射到代碼中的枚舉.在我的 dbml 文件中,當我將類型"設置為 MyTypes.EnumType 時,我收到以下錯誤:

I have a string column in a database table which maps to an Enum in code. In my dbml file when I set the "Type" to MyTypes.EnumType I get the following error:

錯誤 1 ??DBML1005:DbType 'VarChar(50) NOT NULL' 和類型 'Table1' 的列 'EnumCol' 中的類型 'MyTypes.EnumType' 不是支持.

Error 1 DBML1005: Mapping between DbType 'VarChar(50) NOT NULL' and Type 'MyTypes.EnumType' in Column 'EnumCol' of Type 'Table1' is not supported.

這個問題:LINQ to SQL 字符串到枚舉表示我正在嘗試做的事情是可能的,但它是如何完成的?

This question: LINQ to SQL strings to enums indicates that what I am trying to do is possible, but how is it done?

推薦答案

好奇 - 它應該可以工作 IIRC;我會看看我是否可以做一個簡單的例子 - 但是,您可能想要檢查您是否擁有完全限定的枚舉名稱(即包括命名空間).

Curious - it should work IIRC; I'll see if I can do a quick example - however, you might want to check that you have the fully-qualified enum name (i.e. including the namespace).

[更新] 從這里似乎 RTM 版本在解析枚舉時附帶了一個錯誤.建議的一種解決方法(在該頁面上)是添加 global:: 前綴.如果沒有這個解決方法,它對我來說很好用,所以它可能在 3.5 SP1 中得到修復?如果枚舉在同一命名空間中使用非限定名稱,則據稱它在 3.5 中也能正常工作.

[update] From here it seems that the RTM version shipped with a bug when resolving the enum. One workaround suggested (on that page) was to add the global:: prefix. It works fine for me without this workaround, so maybe it is fixed in 3.5 SP1? It also allegedly works fine in 3.5 if you use the unqualified name if the enum is in the same namespace.

[示例] 是的,工作正常:使用 Northwind,我為運輸國家/地區定義了一個枚舉:

[example] Yup, worked fine: with Northwind, I defined an enum for the shipping country:

namespace Foo.Bar
{
    public enum MyEnum
    {
        France,
        Belgium,
        Brazil,
        Switzerland
    }
}

然后我將 dbml 編輯為:

I then edited the dbml to have:

<Column Name="ShipCountry" Type="Foo.Bar.MyEnum" DbType="NVarChar(15)" CanBeNull="true" />

這產生了:

private Foo.Bar.MyEnum _ShipCountry;
//...
[Column(Storage="_ShipCountry", DbType="NVarChar(15)", CanBeNull=true)]
public Foo.Bar.MyEnum ShipCountry
{ get {...} set {...} }

最后寫了一個查詢:

using (DataClasses1DataContext ctx = new DataClasses1DataContext())
{
    var qry = from order in ctx.Orders
              where order.ShipCountry == Foo.Bar.MyEnum.Brazil
                || order.ShipCountry == Foo.Bar.MyEnum.Belgium
              select order;
    foreach (var order in qry.Take(10))
    {
        Console.WriteLine("{0}, {1}", order.OrderID, order.ShipCountry);
    }
}

工作正常;結果:

10250, Brazil
10252, Belgium
10253, Brazil
10256, Brazil
10261, Brazil
10287, Brazil
10290, Brazil
10291, Brazil
10292, Brazil
10299, Brazil

這篇關于從字符串映射枚舉的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 免费在线观看黄视频 | 91精品国产日韩91久久久久久 | 午夜国产精品视频 | 亚洲v日韩v综合v精品v | 日本中文字幕在线视频 | 亚洲精品一区二区在线 | 91精品一区 | 亚洲福利在线观看 | 国产精品亚洲一区二区三区在线 | 国产99久久久久 | 夜夜夜夜夜夜曰天天天 | 成人免费网站视频 | 天天玩天天干天天操 | 女人夜夜春 | 国产在线拍偷自揄拍视频 | 中文字幕一区二区三区日韩精品 | 成人午夜免费视频 | 日本字幕在线观看 | 中文字幕亚洲视频 | 中文字幕一区二区三区四区五区 | 久久久久久九九九九 | 亚洲三区在线 | 97碰碰碰| 激情五月综合 | 色婷婷久久综合 | 欧美视频精品 | 美女视频一区二区 | 三级在线视频 | 99精品久久久 | 视频一区二区三区在线观看 | 亚洲一区免费视频 | 99爱国产 | 欧美性一区二区三区 | 久久免费电影 | 亚洲综合婷婷 | av性色全交蜜桃成熟时 | 在线免费观看黄网 | 丁香综合| www.日日干 | 色播久久久 | 秋霞性生活 |