문제

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