我有一个代表不同内容的ID列表。我想将这些ID显示为字符串。
例如,数字12
将映射到"foo"
,而13会映射到"bar"
更具体地说,我正在尝试将模型属性迁移到包含已转换字符串值的列表。这是我目前的
var model = new PaymentExclusionModel
{
ExclusionList = response.Exclusions,
};
List<PaymentProcessorExclusionModel> list = new List<PaymentProcessorExclusionModel>();
list = response.Exclusions.Select(x => new PaymentProcessorExclusionModel
{
// Convert
});
我知道将整数映射到字符串的值是什么,因为它是一小组值。我试图使用Dictionary<int, string>
,但我收到一个错误抱怨转换int? to int(int?来自元数据dll,我不能改变它)。
以下是代码:
var model = new PaymentExclusionModel
{
ExclusionList = response.Exclusions,
};
List<PaymentProcessorExclusionModel> list = new List<PaymentProcessorExclusionModel>();
list = response.Exclusions.Select(x => new PaymentProcessorExclusionModel
{
PaymentType = dict[x.PaymentTypeId.Value]
});
错误代码:
Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<DK.Fin.Secure.Mjolnir.Web.Models.PaymentProcessorExclusionModel>' to 'System.Collections.Generic.List<Web.Models.PaymentProcessorExclusionModel>'. An explicit conversion exists (are you missing a cast?)
答案 0 :(得分:1)
这是一个枚举式解决方案:
public class MyType : EnumClass
{
public static MyType FOO = new MyType(12, "Foo");
public static MyType BAR = new MyType(13, "Bar");
public static MyType X = new MyType(99, "X");
[JsonConstructor]
private MyType(int id, string name)
{
_id = id;
_name = name;
}
public static MyType GetMyType(int id)
{
switch (id)
{
case 12: return FOO;
case 13: return BAR;
case 99: return X;
default: return null;
}
}
public static IEnumerable<MyType> GetMyTypes()
{
return new List<MyType>
{
FOO,
BAR,
X
};
}
}
我使用过这种类型的东西并取得了巨大的成功。我有一个扑克应用程序,在这种情况下,&#39; MyType&#39;是卡&#39;具有扑克牌的所有属性(套装,号码等)。
EnumClass
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyProj.MyPath
{
public abstract class EnumClass
{
protected int _id;
protected string _name;
public virtual int ID
{
get { return _id; }
}
public virtual string Name
{
get { return _name; }
}
public override string ToString()
{
return Name;
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is EnumClass)
return ID == (obj as EnumClass).ID;
return base.Equals(obj);
}
public static bool operator ==(EnumClass left, EnumClass right)
{
if ((left as Object) == null)
return (right as Object) == null;
return left.Equals(right);
}
public static bool operator !=(EnumClass left, EnumClass right)
{
if ((left as Object) == null)
return (right as Object) != null;
return !left.Equals(right);
}
}
}
答案 1 :(得分:-1)
我不知道为什么,但我相信你想在字符串和int值之间创建一个数组映射。 首先,您需要使用字典
Dictionary<int, string> map = new Dictionary<int, string>();
然后只需添加您的值
map.Add(12, 'foo');
map.Add(13, 'bar');
然后基本上你所要做的就是像字典一样调用字典
list = response.Exclusions.Select(x => new PaymentProcessorExclusionModel
{
NewId = map.ContainsKey(x) ? map[x] : "defaultkey?"
});
这显然是要走的路。尝试创建“&#39;”列表&#39;动态变量如此
var list = response.Exclusions.Select(x => new PaymentProcessorExclusionModel
{
PaymentType = dict[x.PaymentTypeId.Value]
});