2
public ActionResult Hotel_Read(string text)
{
    var result = GetHotel().Where(c => c.Name.Contains(text) || c.City.Contains(text) || c.Country.Contains(text)).ToList();
    return Json(result, JsonRequestBehavior.AllowGet);
}

private static IEnumerable<HotelViewModel> GetHotel()
{
    using (TravelAgancyEntities1 db = new TravelAgancyEntities1())
    {
        var query = db.Hotels
                      .Include(p => p.City.Country).Distinct().ToList();

        return query.Select(Hotel => new HotelViewModel
         {
             Name = Hotel.Name,
             City = Hotel.City.City_Name,
    **Line 10-** Country = Hotel.City.Country.Country_Name,//!!!

         });
    }
}

When I run the code without line 10, it is working successfully, but when that code is run with line 10, then it's not working.

enter image description here

5
  • You know we can't see the line numbers and we're loathe to count them ourselves? Also, what's the error? Commented Apr 7, 2017 at 10:48
  • you cant do this as you are doing. qury has object with hotels only. Commented Apr 7, 2017 at 10:52
  • what you actually want in your code and in result please be brief your question. Commented Apr 7, 2017 at 10:53
  • Hotel.City.Country. is collection of countries. you need to iterate on it to get single instance. Commented Apr 7, 2017 at 10:59
  • 1
    Include(p => p.City.Select(x => x.Country)).Distinct().ToList(); Commented Apr 7, 2017 at 11:00

2 Answers 2

1

I assume your code should run properly. the only thing that makes me suspicious, is that you are trying to retrieve all Hotel table data plus 2 other table(with include)
try this :

var q = (from x in db.Hotels.Include(c => c.City).Include(c =>  c.City.Country)
                         where x.Id == 5030
                         select x).Distinct().ToList();
string s = q[0].City.Country.Country_Name;

Limit your select with Where clause.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need Includes here because there is no application logic involved in creation of HotelViewModel instances.

Simple query:

db.Hotels.Select(h => new HotelViewModel
         {
             Name = h.Name,
             City = h.City.City_Name,
             Country = h.City.Country.Country_Name,
         }).ToList();

Will return from the DB exactly the data that you need.

When you first do Includes, and then call ToList();:

var query = db.Hotels.Include(p => p.City.Select(x => x.Country))
                     .Distinct()
                     .ToList(); 

You fetch form DB all the Hotel properties, all the City properties and all the Country properties while all you really need is only their names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.