Tag: 枚举

如何在entity framework中使用枚举?

在Entity Framework中使用Enums的最佳方式是什么? 备注:我正在使用EF 3和Firebird。

如何序列化一个枚举值作为一个int?

我想序列化我的枚举值作为一个int,但我只得到这个名字。 这是我的(样本)类和枚举: public class Request { public RequestType request; } public enum RequestType { Booking = 1, Confirmation = 2, PreBooking = 4, PreBookingConfirmation = 5, BookingStatus = 6 } 和代码(只是为了确保我没有做错) Request req = new Request(); req.request = RequestType.Confirmation; XmlSerializer xml = new XmlSerializer(req.GetType()); StringWriter writer = new StringWriter(); xml.Serialize(writer, req); textBox1.Text = writer.ToString(); 这个答案 […]

按键进行散列并对值进行求和

我有一个哈希数组: [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}] 我想在这里使用inject ,但是我一直在挣扎。 我想要一个新的散列,它反映了以前散列的重复键的总和: [{"Vegetable"=>15}, {"Dry Goods"=>5}] 我在控制输出这个散列的代码,所以我可以修改它,如果有必要。 结果主要是散列,因为这可能最终嵌套任意数量的层次,然后很容易在数组上调用flatten,但是也不能将hash的键/值弄平: def recipe_pl(parent_percentage=nil) ingredients.collect do |i| recipe_total = i.recipe.recipeable.total_cost recipe_percentage = i.ingredient_cost / recipe_total if i.ingredientable.is_a?(Purchaseitem) if parent_percentage.nil? {i.ingredientable.plclass => recipe_percentage} else sub_percentage = recipe_percentage * parent_percentage {i.ingredientable.plclass => sub_percentage} end else i.ingredientable.recipe_pl(recipe_percentage) end end end

如何为Enum值TryParse?

我想写一个函数,可以validation一个给定的值(作为string传递)与可能的enum值。 在匹配的情况下,它应该返回枚举实例; 否则,它应该返回一个默认值。 该函数可能不会在内部使用try / catch ,不包括使用Enum.Parse ,当给定一个无效参数时抛出一个exception。 我想使用一些TryParse函数来实现这个function: public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue) { object enumValue; if (!TryParse (typeof (TEnum), strEnumValue, out enumValue)) { return defaultValue; } return (TEnum) enumValue; }

保存从select导轨4.1枚举

我使用Rails 4.1中的枚举来跟踪葡萄酒的颜色。 Wine.rb class Wine < ActiveRecord::Base enum color: [:red, :white, :sparkling] end 在我看来,我产生了一个select,所以用户可以select具有某种颜色的葡萄酒 f.input :color, :as => :select, :collection => Wine.colors 这将生成以下HTML: <select id="wine_color" name="wine[color]"> <option value=""></option> <option value="0">red</option> <option value="1">white</option> <option value="2">sparkling</option> </select> 但是,提交表单后,我收到一个参数错误,指出'1' is not a valid color 。 我意识到这是因为color必须等于1而不是"1" 。 有没有办法强制Rails将颜色解释为整数而不是string?

在Go中表示枚举的惯用方式是什么?

我试图表示一个简化的染色体,它由N个碱基组成,每个碱基只能是{A, C, T, G} 。 我想用enum来forms化约束,但是我想知道在Go中模拟枚举最常用的方式是什么。

Ruby中的“map”方法是做什么的?

我是编程新手。 有人可以解释一下.map会在下面做些什么: params = (0…param_count).map

Javagenerics和枚举,模板参数的丢失

我有一个相当复杂的结构,并没有按预期工作。 这就是我所做的: public interface ResultServiceHolder { <M, ID extends Serializable, BO extends BusinessObject<M, ID>> ResultService<M, ID, BO> getService(); } public enum ResultTypes implements ResultServiceHolder { RESULT_TYPE_ONE { @Override public ResultOneService getService() { //unchecked conversion? return serviceInitializer.getResultOneService(); } }, RESULT_TYPE_TWO { @Override public ResultTwoService getService() { //unchecked conversion? return serviceInitializer.getResultTwoService(); } }, RESULT_TYPE_THREE { @Override public […]

如何将字符转换为等效的System.Windows.Input.Key枚举值?

我想写这样的function, public System.Windows.Input.Key ResolveKey(char charToResolve) { // Code goes here, that resolves the charToResolve // in to the Key enumerated value // (For example with '.' as the character for Key.OemPeriod) } 我知道我可以写一个巨大的开关箱来匹配angular色,但是有没有其他方法? 与此有关的是Key枚举的string可能与字符不匹配,所以Enum.IsDefined将不起作用 有任何想法吗? 更新:这是在Windows环境中

在枚举types上实现`next`和`previous`的最好方法是什么?

假设我有一个枚举: enum E { A, B, C; } 如Lucasmo的 答案所示,枚举值按照它们初始化的顺序存储在一个静态数组中,稍后可以使用E.values()检索(克隆)该数组。 现在假设我想要实现E#getNext和E#getPrevious ,使得以下所有expression式都计算为true : EAgetNext() == EB EBgetNext() == EC ECgetNext() == EA EAgetPrevious() == EC EBgetPrevious() == EA ECgetPrevious() == EB 我目前的getNext实现如下: public E getNext() { E[] e = E.values(); int i = 0; for (; e[i] != this; i++) ; i++; i %= e.length; […]