質問

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