我有一个枚举:
foo = (PrivilegedAccounts) (rand());
我希望得到一个与用户不同的值。 我知道有这个兰特(不确定是真的):
CREATE TRIGGER updatehargaahs
ON tbl_hitash
AFTER INSERT
AS
UPDATE tbl_ahs
SET harga = (SELECT SUM(harga_satuan)
FROM tbl_hitash
WHERE tbl_ahs.id_ahs = tbl_hitash.id_ahs)
WHERE id_ahs = inserted.id_ahs
但如何使其除了“user”以外的任何值?
谢谢,
答案 0 :(得分:3)
我希望这就是你要找的东西。
public enum PrivilegedAccounts
{
User = 1,
Service = 16,
Admin = 64,
Other = 1040,
Executive = 2048
}
var values = Enum.GetValues(typeof(PrivilegedAccounts));
SomeEnum randomValue = (PrivilegedAccounts)values[Random.Range(1, values.Length)]; //Start from 1 instead of 0 to avoid User
答案 1 :(得分:0)
尝试这样的事情:
Random random = new Random();
int range_min = 1;
int range_max = 3;
int randomNumber;
do
{
randomNumber = random.Next(range_min, range_max);
} while (randomNumber == (Int32)PrivilegedAccounts.User);
答案 2 :(得分:0)
1-首先计算Enum的数量
2-在计数之间设置随机数
3-创建Enum列表
4-返回Enum的索引
Random rnd = new Random();
var myEnumMemberCount = Enum.GetNames(typeof(PrivilegedAccounts)).Length; // Step1
int count = rnd.Next(1, myEnumMemberCount); // creates a number between 1 and PrivilegedAccounts count //Step2 User is ignored
Array eVals = Enum.GetValues(typeof(PrivilegedAccounts)); //Step3
PrivilegedAccounts foo = (PrivilegedAccounts)eVals.GetValue(count); //Step4
答案 3 :(得分:0)
您可以创建一个方法:
static PrivilegedAccounts GetRandPrivAccountExcluding(
PrivilegedAccounts excludedValue, Random rand)
{
var privAccounts = Enum.GetValues(typeof(PrivilegedAccounts))
.OfType<PrivilegedAccounts>()
.Where(x => x != excludedValue)
.ToList();
return privAccounts[rand.Next(privAccounts.Count)];
}
使用示例:
static void Main(string[] args)
{
Random rand = new Random();
var randPriviledge = GetRandPrivAccountExcluding(PrivilegedAccounts.User, rand);
}