I have this 'method' :

private static readonly Expression<Func<EntityClass, SomeClass>> AsSomeClass =
        x => new SomeClass
        {

        };

which lets me do :

_ctx.EntityClasses.Where(e => e.SomeProperty == someValue).Select(AsSomeClass);

which returns a IQueryable<SomeClass> which is great!

While doing it I was wondering is there is some way to make it so I can do :

_ctx.EntityClasses.Where(e => e.SomeProperty == someValue).AsSomeClass() instead of the select statement.

Is there any way to make it that way?

有帮助吗?

解决方案

You can do that by making AsSomeClass an extension method on IEnumerable<EntityClass> (or on IQueryable<EntityClass> as well, but in this instance that does not buy you anything), but please don't.

.Select(ConversionFunction) is totally clear on the reader; .AsSomeClass() is rather opaque: it could be doing pretty much anything. Could you tell if for example it casts the contents of sequence or if it projects them, or even if it changes the type of the sequence itself?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top