i have a linq query for fetch data from 3 table (Post, Category, Tag).
i want show latest posts (10 num) in default page of a blog: post title, post tags name and post category name, post short description. i type this linq query
List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
{
using (var context = new MJBweblogContext())
{
IQueryable posts = from Post in context.Posts
where Post.Published == true
select new
{
Post.Title,
Post.ShortDescription,
Post.Description,
Post.MetaData,
Post.PostedOn,
Post.UrlSlug,
Post.Published,
Post.Modified,
Post.Category,
Post.Tags,
Post.Category.Name,
};
if (posts != null)
{
return posts.OfType<Post>() // i give error if i remove OfType<Post>()
.OrderByDescending(p => p.PostedOn)
.Skip(pageNo * pageSize)
.Take(pageSize)
.ToList();
}
else
{
return null;
}
}
}
but i have two problem:
if i remove OfType() VS2017 tell me:
Error CS0266 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) MJBweblog C:\Users\m.j.b\source\repos\MJBweblog\MJBweblog\DAL\BlogRepository.cs 44 Active
- I don't know what code I should write to fetch tags for each post and their category? in the linq query? (i try to show user something like this template of each post in default page)
this is What I want to show for users in default page. (a list of this)

posts. You should define a custom ViewModel which holds all the properties you want to pass to the view and project it.postsvariable is nevernull