Reputation: 55032
public class News
{
public virtual int Id { set; get; }
public virtual string Title { get; set; }
public virtual string Thumbnail { set; get; }
public virtual string Image { set; get; }
public virtual string NewsContent { set; get; }
public virtual DateTime DateCreated { set; get; }
public virtual bool Published { set; get; }
public virtual int UserCreated { set; get; }
public virtual Category Category { set; get; }
public virtual DateTime DateUpdated { set; get; }
public virtual int ViewCount { set; get; }
}
I have this class and I d like to query the database to get results. here is my query :
foreach (var category in categories)
{
var news = newsRepo.Query("from News n where n.Category ="+category);
}
here is the Query method.
public IQueryable<T> Query(string query)
{
IQueryable<T> queryable;
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var hql = session.CreateQuery(query);
var list = hql.List<T>();
queryable = list.AsQueryable();
transaction.Commit();
}
return queryable;
}
and it doesnt work.
I get the following error:
could not execute query
[ select news0_.Id as Id13_, news0_.Title as Title13_, news0_.DateCreated as DateCrea3_13_, news0_.DateUpdated as DateUpda4_13_, news0_.NewsContent as NewsCont5_13_, news0_.Published as Published13_, news0_.UserCreated as UserCrea7_13_, news0_.Image as Image13_, news0_.Thumbnail as Thumbnail13_, news0_.ViewCount as ViewCount13_, news0_.Category_id as Category11_13_ from [News] news0_ where news0_.Category_id=. ]
How can i fix it?
any ideas?
Upvotes: 1
Views: 309
Reputation: 16262
If you put a break point at the following line of code then you should see that the category
variable isn't getting initialized to what you expect (which should be an integer):
var news = newsRepo.Query("from News n where n.Category ="+category);
That aside, there are several better ways of facilitating this query. One is to just use Session.Get(id) like so:
var news = Session.Get<News>(1);
You could also use the NHibernate LINQ provider like so:
var news = Session.Query<News>().SingleOrDefault();
Only use literal SQL or HQL when absolutely necessary.
Upvotes: 1