Select method is not called using LinQ query syntax

前端 未结 3 1853
一个人的身影
一个人的身影 2021-01-20 01:53

I want to enable LinQ query syntax over my classes. I think query syntax is translated to method syntax, for example:

var query = from p in new Class1

        
3条回答
  •  庸人自扰
    2021-01-20 02:01

    I'm pretty sure that the translation from query syntax to method syntax optimizes the call to Select away if it states an identity projection.

    Since p => p would project everything to itself and the Where clause already added an abstraction layer between the source sequence and the result, this call is no longer necessary.

    So

    var query = from p in new Class1()
                where p.Id == "1000"
                select p;
    

    is only translated to

    var query = new Class1().Where(p => p.Id == "1000");
    

    But I admit that I only guess that and am still searching for the relevant specification part.
    Update: Jon was faster

提交回复
热议问题