asp.net mvc - MVC EF first result query is replaced by second result if second result got the same record -
i used entity framework 5. had table hotel had collection of roomtypes.
when wanted find hotel had following roomtypes looped function each roomtype , merged result. first result replaced second result if contained same record.
model public class hotel { public int id { get; set; } public string hotelname { get; set; } [foreignkey("hotelid")] public icollection<roomtype> roomtypes { get; set; } } public class roomtype { public int id { get; set; } public string name { get; set; } public int hotelid { get; set; } } caller public jsonresult gethotelbyroomtypes(string[] roomtypenames) { list<list<hotel>> hotels = new list<list<hotel>>(); foreach (string roomtypename in roomtypenames) { list<hotel> partialhotel = hotelrepo.gethotelsfromroomtype(roomtypename); hotels.add(partialhotel); } return json(hotels); } hotelrepo public list<hotel> gethotelsfromroomtype(string roomtype) { list<roomtype> roomtypes = db.roomtypes.where(r => r.name.equals(roomtype)).tolist(); copy hotelid roomtypes int[] hotelids list<hotel> hotels = db.hotels.where(h => hotelids.contains(h.id)).tolist(); return hotels }
my problem if sent 2 roomtypes ["deluxe", "suite"]. got list of hotela , hotelb first result. both of them contained collection of roomtype of "deluxe". after that, put these 2 hotels list>.
then second result returned hotela , hotelc hotela contained collection of roomtype "suite".
when happended, hotela of first result same hotela of second result held "suite" in roomtypes collection , "deluxe" in hotela missing.
do have suggestions?
use addrange method. can cleanup repo function.
public jsonresult gethotelbyroomtypes(string[] roomtypenames) { list<list<hotel>> hotels = new list<list<hotel>>(); foreach (string roomtypename in roomtypenames) { list<hotel> partialhotel = hotelrepo.gethotelsfromroomtype(roomtypename); hotels.addrange(partialhotel); } return json(hotels); } public list<hotel> gethotelsfromroomtype(string roomtype) { list<hotel> hotelswithroomtype = db.hotels .include(h => h.roomtypes) .where(h => h.roomtypes.any(r => r.name.equals(roomtype)) .tolist(); return hotels; }
https://msdn.microsoft.com/en-us/library/z883w3dc%28v=vs.110%29.aspx?f=255&mspperror=-2147217396
Comments
Post a Comment