3 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
我不知道它是什么db,為什么不想要使用它enum(因?yàn)槟梢宰龅竭@一點(diǎn),請(qǐng)參閱下文),但這也許可以幫助您:
public class Countries
{
public const string India = "India";
...
}
或者,如果您想要國(guó)家/地區(qū)的ID,則可以使用
public class Countries
{
public const int India = 42;
...
}
可以通過(guò)以下方式訪問這兩種建議的方法:
var india = Countries.India;
我知道沒有人問這個(gè)問題,但是由于問題表明該問題可能是XY問題,因此我建議您采用以下方法:
假設(shè)您的國(guó)家/地區(qū)表如下所示:
CountryID | Name
------------------------------------
1 | India
42 | United States of America
那么您可以創(chuàng)建一個(gè)如下所示的枚舉:
public enum Country
{
India = 1,
[Description("United States of America")]
United States = 42
}
而且您不會(huì)丟失任何信息。

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果您想讓智能感知向您顯示集合中的數(shù)據(jù),那不是它的工作原理。您的C#源代碼無(wú)法使用數(shù)據(jù)庫(kù)中的數(shù)據(jù),因?yàn)閿?shù)據(jù)庫(kù)中的數(shù)據(jù)可能會(huì)更改,而您的代碼仍會(huì)引用某些固定名稱屬性。
如果您想在按下點(diǎn)號(hào)'。'時(shí)向intellisense顯示一些可用的功能(擴(kuò)展功能),則必須在文件開頭包含在其中定義這些功能的名稱空間,以使intellisense顯示該功能??赡苄?。
到目前為止,尤其是執(zhí)行驗(yàn)證最有用的方法之一是使用:
using System.Linq;
在文件的開頭
然后,您可以使用像這樣的東西:
if (db.Countries.Any(c => c.Name == "India" ) {
// this block will execute only if at least one country is India
}
foreach (var country in db.Countries.Where(c => c.Name == "India")) {
// this block will execute for each element in countries that has Name equals India and attach the country to some variable so you can perform more work with it
}
如果您希望以某種方式擁有固定值列表(例如枚舉),則一些有趣的方法是使用常量,這已在Thomas的答案中得到了證明。
- 3 回答
- 0 關(guān)注
- 213 瀏覽
添加回答
舉報(bào)