English 中文(简体)
Linq To Sql: 增加价值转变
原标题:Linq To Sql : add value transformation

考虑以下表述:

from p in db.People
    select new Person {
        Name = p.Name, Age = p.Age, Gender = p.Gender.ToEnum<Gender>()
};

It works to the point of calling extension method static T ToEnum<T>(this string value); as expected.
I understand why there is do not know how to translate string to enum error.
Question is: how do I work around this without introducing another class?
I mean that I could define class PersonWithGenderAsText and then translate that into Person class, but I feel there got to be an easier way.
Specifically, I don t mind calling .ToList() on the result of the expression above, but I cant figure out the rest.

最佳回答

仅使用匿名内身打字,仅指你希望从数据库中提取的比照,使用可折成“在过程”方式,并then 您可做转换成个人物体:

var query = db.People
              .Select(p => new { p.Name, p.Age, p.Gender } )
              .AsEnumerable()
              .Select(p => new Person { Name = p.Name,
                                        Age = p.Age,
                                        Gender = p.Gender.ToEnum<Gender>() } );

如果在<代码>db.People上没有其他数据,你可以进一步简化:

var query = db.People
              .AsEnumerable()
              .Select(p => new Person { Name = p.Name,
                                        Age = p.Age,
                                        Gender = p.Gender.ToEnum<Gender>() } );
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签