Reputation: 2585
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
Upvotes: 0
Views: 306
Reputation: 7352
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
});
Upvotes: 0
Reputation: 1628
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
})
Upvotes: 0
Reputation: 59705
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.
Upvotes: 3
Reputation: 1466
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
Upvotes: 0