English 中文(简体)
lin lin lin MVC 2.0
原标题:Parse string to poco class enum in linq query MVC 2.0

我有以下一席和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,
       };

不幸的是,我犯了一个错误,即“没有含蓄地将扼制为模型的类型转换成模型。 两性平等

我愿将实体框架所编的插座改为我的性别系,并将其分配给我的组织。

最佳回答

在实体框架中,Enums没有得到支持。 亚历克·詹姆斯进行了工作,但这项工作相当多。

相反,我更愿意这样做:

public enum Gender : byte
{
    Male = 1,
    Female,
    Unknown
}

public class Person
{
    public int PersonId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public byte Gender { get; set; } // this is the EF model property
    public Gender GenderType // this is an additional custom property
    { 
        get { return (Gender) Gender; }
        set { Gender = (byte)value; }
    }
}

它对实际价值来说基本上是一个 h/rap。 在您的数据库中,将性别问题作为<代码>tinyint(在概念方面,将性别分布在byte上)。

之后,你可以使用星号绘制和从模型财产中取出:

return from p in _db.People
       select new Model.Person
       {
          PersonId = p.PersonId,
          LastName = p.LastName,
          FirstName = p.FirstName,
          Gender = p.Gender, // sets byte
       };

但是,如果你能够查阅这一观点,因为你为<代码>Gender确定了星号,那么你也将有机会查阅财产编码

这是否解决了你的问题?

问题回答

我所熟悉的实体框架没有为论坛提供支持。 EF利用你的问询,制作一份显示文件,然后发送给服务器,如果它不能建立某种操作的等同结构,它就会对这一行动不支持。 如果您希望能够退还少量数据,你可以通过使用<代码>ToArray方法创建用于记忆的物体,与实体框架分开。

var myEntities = (from entity in _db.Entities
                  where /* condition */
                  select entity)
                        .ToArray();

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:

return from myEntity in myEntities
       select new MyDataContract
       {
            ID = myEntity.ID,
            Gender g = (Gender)Enum.Parse(typeof(Gender), myEntity.Gender, true)
       };

或者,你甚至可以将其细分为<代码>foreach。 住宿:

List<MyDataContract> myDataContracts = new List<MyDataContract>();
foreach (var myEntity in myEntities)
{
    var dataContract = new MyDataContract { ID = myEntity.ID };

    if (Enum.IsDefined(typeof(Gender), myEntity.Gender))
        dataContract.Gender = (Gender)Enum.Parse(typeof(Gender), myEntity.Gender, true);

        myDataContracts.Add(dataContract);
}

return myDataContracts.AsEnumerable();
   if (Enum.IsDefined(typeof(Gender), genderstring))
      Gender g = (Gender) Enum.Parse(typeof(Gender), genderstring, true);
   else
      //Deal with invalid string.

try

Gender = p.Gender != null ? (Gender)Enum.Parse(typeof(Gender), p.Gender) : (Gender?)null;

To parse the string as one of the enums

它意味着改变你的冰和干净的POCO

http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx”rel=“nofollow”http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx





相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...

热门标签