0

I am using Linq to Entities and I have a query something like this

context.Hotels
        .Where(h => h.HotelType.Contains(s.HotelTypeId.ToString()))
        .Select(hotel => new Model.Hotel
           {
              HotelId = hotel.HotelID,
              HotelName = hotel.HotelName,
              HotelFileName = hotel.HotelFileName,
              StarRating = hotel.StarRating,
              CountryName = hotel.Country.CountryName,
              PlaceName = hotel.Place.PlaceName
           })

I am using .ToString() in where clause which I know is not valid when work with Linq To Entities. But actually "HotelType" column have values separated with pipe characters like 1|2|3..Now I want to extract only those Hotels that have a type 1..How is it possible? Please help

3
  • 3
    First of all, you must change your tables structure. It is not good to save 1|2|3 in one cell. You can create another table with the name HotelTypes with at least two columns: Hotel_ID and Type_ID. Then use join to this table. Commented May 10, 2014 at 11:05
  • I have a very large database, it is quite difficult at this moment. I am wondering if somehow i can make it possible Commented May 10, 2014 at 11:06
  • Consider making the question title more specific. This one is really not describing your problem at all. Commented May 10, 2014 at 11:06

4 Answers 4

3

It's a hack but it should work.

Where(h => ("|" + h.HotelType + "|").Contains("|" + s.HotelTypeId.ToString() + "|"))

It first turns 1|2|3 into |1|2|3| and then looks for |1| or |2| and will work no matter if the ID is first or last or somewhere in the middle.

But you really should restructure your database - it is usually a very bad idea to have information encoded like that and you already found out because you had to ask how to do something that should be trivial.

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

2 Comments

I am getting this error now: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
Add back the ToString() call - I thought you can leave it out but maybe not.
0

It's also worth noting that ToString() is supported in EF 6.1 http://blogs.msdn.com/b/adonet/archive/2014/03/17/ef6-1-0-rtm-available.aspx

Comments

0
context.Hotels
    .Where(h => h.HotelType.Split('|').Select(str=>Convert.ToInt32(str)).Contains(s.HotelTypeId))
    .Select(hotel => new Model.Hotel
       {
          HotelId = hotel.HotelID,
          HotelName = hotel.HotelName,
          HotelFileName = hotel.HotelFileName,
          StarRating = hotel.StarRating,
          CountryName = hotel.Country.CountryName,
          PlaceName = hotel.Place.PlaceName
       })

Comments

0

Assuming your HotelType is int you can do this.

context.Hotels
    .Where(h => h.HotelType.Split("|").Select(ht=>int.Parse(ht))
    .Contains(s.HotelTypeId))
    .Select(hotel => new Model.Hotel
       {
          HotelId = hotel.HotelID,
          HotelName = hotel.HotelName,
          HotelFileName = hotel.HotelFileName,
          StarRating = hotel.StarRating,
          CountryName = hotel.Country.CountryName,
          PlaceName = hotel.Place.PlaceName
       });

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.