跟进this问题。
我想从Strongly Typed
投射到Anonymous Type
结果。例如,以下类应在运行时转换为匿名类型对象。我正在努力实现这一目标。
public sealed class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public bool IsActive { get; set; }
}
用法:
new CountryModel()
{
CountryCode = "AOE",
CountryId = 2,
CountryName = "Anywhere on Earth",
IsActive = true
};
匿名类型:
以上强类型应该转换为匿名,最终结果如下(通过立即窗口捕获):
{ CountryId = 2, CountryName = "Anywhere on Earth", CountryCode = "AOE", IsActive = true }
CountryCode: "AOE"
CountryId: 2
CountryName: "Anywhere on Earth"
IsActive: true
注意:我需要完成此转换,以便将对象传递给Dapper.SimpleCRUD和Dapper ORM库。
答案 0 :(得分:2)
试试这个:
var obj = new {
CountryCode = item.CountryCode,
CountryId = item.CountryId,
CountryName = item.CountryName,
IsActive = item.IsActive
};