public IQueryable <PostModel> GetByTagId([FromUri] int tagId, [FromUri] string sessionKey) { var user = this.userRepository .GetAll().Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new InvalidOperationException("The user is not logged in"); } var tag = this.tagRepository.GetById(tagId); if (tag == null) { throw new InvalidOperationException("There is no tag with id=" + tagId); } var postModels = new List <PostModel>(); var postEntities = tag.Posts; foreach (var postEntity in postEntities) { postModels.Add(PostModel.CreateFromPostEntity(postEntity)); } return(postModels.OrderByDescending(p => p.PostDate).AsQueryable()); }