c# - select item with filtered child items with linq -
i have complicated hierarchy of elements filter downwards based on kid item 4 deep.
here sample of classes
class hotel { //other properties omitted brevity public list<room> rooms{get;set;} } class room { //other properties omitted brevity public list<roomtype> roomtypes{get;set;} } class roomtype { //other properties omitted brevity public list<price> prices {get;set;} } class cost { //other properties omitted brevity decimal totalprice{get;set;} }
so hotel top level have rooms
, have number of roomtypes
have number of prices
i want filter out other cheapest totalprice , relating parents in each instance keep hierarchy in place, leave number of hotels rooms, roomtypes , minimum price
var filteredhotels = hot in resp.hotels allow types = hot.rooms.selectmany(rooms => rooms.roomtypes) allow prices = types.selectmany(t => t.prices) select new { hot , types , minprice = prices.min(p => p.totalprice) };
but of course of study doesn't work.
in response comments, require properties on classes in hierarchy. want filter out multiple expensive prices. , think single room have single price, each room can set differently have different prices. plus not hierarchy, service consuming.. , sorry resp response service, has hotels object in it. , can ignored..
so clear (i hope), need hotels object, filtered list of children below leaving me single cheapest totalprice below it.. hoping avoid having project properties of hierarchy wanted maybe impossible not to
thanks help
would work you?
var filteredhotels = resp.hotels.select(h => h.rooms.select(r => r.roomtypes.select(rt => new { hotelname = h.name, roomname = r.name, roomtypename = rt.name, minprice = rt.prices.min(p => p.totalprice) })));
assuming classes have name property. can replace property whatever want.
class hotel { public string name { get; set; } //other properties omitted brevity public list<room> rooms { get; set; } } class room { public string name { get; set; } //other properties omitted brevity public list<roomtype> roomtypes { get; set; } } class roomtype { public string name { get; set; } //other properties omitted brevity public list<price> prices { get; set; } } class cost { public string name { get; set; } //other properties omitted brevity public decimal totalprice { get; set; } }
c# .net linq-to-objects
No comments:
Post a Comment