开发者

Convert to dot notation

开发者 https://www.devze.com 2023-02-17 20:49 出处:网络
How can I convert this query to use Extension Methods? var x = from Prods 开发者_如何学JAVAn in Cat.Prod.GetAllProds()

How can I convert this query to use Extension Methods?

var x = from Prods 开发者_如何学JAVAn in Cat.Prod.GetAllProds()
        orderby n.Name
        select new
        {
            Name = n.Name,                        
            Cost = n.Cost
        };


Its called Lambda notation.

var x = Cat.Prod.GetAllProds().OrderBy(n=>n.Name).Select(n=>new {n.Name,n.Cost});

Note that you do not need to provide a name for each column you are selecting if that name is the same as the column name:

new
{
    Name = n.Name,                        
    Cost = n.Cost
});

Is exactly the same as:

new
{
    n.Name,                        
    n.Cost
});


It's quite simple in this case:

var x = Cat.Prod
           .GetAllProds()
           .OrderBy(n => n.Name)
           .Select(n => new
                        {
                           Name = n.Name,                        
                           Cost = n.Cost
                        });

For more information, I suggest reading How query expressions work - Jon Skeet: Coding Blog.

0

精彩评论

暂无评论...
验证码 换一张
取 消