开发者

Convert to dot notation

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜