在linq查询MVC 2.0中将字符串解析为poco类枚举
我有以下枚举和 POCO 类,
public enum Gender
{
Male,
Female,
Unknown
}
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public Gender? Gender { get; set; }
}
我想在我的存储库中执行“获取所有人”查询,使其看起来像这样:
return from p in _db.People
select new Model.Person
{
PersonId = p.PersonId,
LastName = p.LastName,
FirstName = p.FirstName,
Gender = p.Gender,
};
不幸的是,我收到错误“无法将类型‘string’隐式转换为‘Model.Gender’ '"
我想将从实体框架查询的字符串转换为我的 Gender 枚举并将其分配给我的 POCO 类。
I have the following enum and POCO class
public enum Gender
{
Male,
Female,
Unknown
}
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public Gender? Gender { get; set; }
}
I would like to perform a "get all people" query in my repository such that it would look something like this:
return from p in _db.People
select new Model.Person
{
PersonId = p.PersonId,
LastName = p.LastName,
FirstName = p.FirstName,
Gender = p.Gender,
};
Unfortunately I get an error "Cannot implicitly convert type 'string' to 'Model.Gender'"
I would like to convert the string which is being queried from the entity framework to my Gender enum and assign it to my POCO class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实体框架不支持枚举。 Alex James 有一个解决方法,但是非常复杂。
相反,我更喜欢这样做:
它基本上是实际值的挂钩/包装器。在数据库中,将 Gender 存储为
tinyint
(在概念方面映射到byte
)。然后,您可以使用字节枚举来映射到模型属性或从模型属性映射:
但是,如果您访问该 ViewModel,因为您为
Gender
设置了字节字段,您还可以访问枚举属性<代码>性别类型。这能解决你的问题吗?
Enums are not supported in Entity Framework. There is a workaround by Alex James, but it's quite involved.
Instead, i prefer to do this:
It's basically a hook/wrapper for the actual value. In your database, store Gender as a
tinyint
(which maps tobyte
on the conceptual side).Then you can use a byte enum to map to and from the model property:
But then if you access that ViewModel, because your setting the byte field for
Gender
, you will also have access to the enum propertyGenderType
.Does that solve your problem?
我熟悉的实体框架不提供对枚举的支持。 EF 使用您的查询表达式创建 SQL 语句,然后将其发送到服务器,如果它无法创建某些操作的 SQL 等效项,它将针对该操作抛出 NotSupportedException。如果您希望返回一小组数据,则可以使用 ToArray 方法在内存中创建对象,从而与实体框架分离。
这将在内存中创建一系列实体。然后,任何进一步的查询语句都将位于 LINQ to Objects 的范围内,它允许将字符串解析为枚举:
或者您甚至可以将其分解为
foreach
循环:The Entity Framework that I am familiar with does not provide support for enums. EF uses your query expression to create an SQL statement that it then sends to the server, if it cannot create the SQL equivalent of some operation it will throw a NotSupportedException for that operation. If you are expecting to return a small set of data you can separate from the Entity Framework by creating an object in memory using the
ToArray
method.This will create a sequence of entities in memory. Any further query statements will then be in the realm of LINQ to Objects which allows parsing of strings into enums:
Or you could even break it out into a
foreach
loop:<罢工>
try
将字符串解析为枚举之一
这里有一个解决方法,但这意味着改变你漂亮干净的 POCO
http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake -enums-in-ef-4.aspx
try
To parse the string as one of the enums
here's a workaround but it means changing your nice and clean POCO
http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx