开发者

Order by in LINQ method syntax

开发者 https://www.devze.com 2023-04-12 14:33 出处:网络
I have tried inserting an 开发者_运维百科orderby into this query in every conceivable spot, but can\'t get it to work. In this linq query, I am pulling a list of unitMixes related to a single property

I have tried inserting an 开发者_运维百科orderby into this query in every conceivable spot, but can't get it to work. In this linq query, I am pulling a list of unitMixes related to a single property. There is a field called createDate in the unitMixes table that I would like to order the results by, this seems simple but I just can't quite get my mid around method syntax yet.

var viewModel = new propertyDetailData();
viewModel.properties = db.properties.Where(s => s.propertyId == id).Single();
viewModel.unitMixes = db.properties.Where(s => s.propertyId == id).Single().unitMixes;

Thanks in advance, john


viewModel.properties = db.properties.Single(s => s.propertyId == id);
viewModel.unitMixes = db.properties.Single(s => s.propertyId == id).unitMixes
                                       .OrderBy(m => m.createDate)
                                       .ToList();

You can do the orderby on the list. You don't need to get the list and then do .Single() you can search the Single() element that equals your delegate


viewModel.unitMixes = db.properties.Where(s => s.propertyId == id)
                                   .Single().unitMixes
                                   .OrderBy(m => m.createDate)
                                   .ToList();


viewModel.properties = db.properties.Where(s => s.propertyId == id).Single();
viewModel.unitMixes = viewModel.properties.unitMixes.OrderBy(u => u.createDate);
0

精彩评论

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

关注公众号